throttle_drv.c 13.2 KB
Newer Older
Riccardo Gaiati's avatar
Riccardo Gaiati committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "throttle_drv.h"
#include "ethernet_drv.h"
#include <errno.h>
#include "display.h"
#include "stdlib.h"
#include <string.h>
#include <time.h>
#include <arpa/inet.h>

#define THROTTLENET_PROTO 0x544e
#define ETH_ADR_LEN   6

#define THR_DST_ADR_POS      0
#define THR_SRC_ADR_POS      (THR_DST_ADR_POS  + sizeof(((thr_header_t*)0)->dst_adr))             //6
#define THR_ETH_TYP_POS      (THR_SRC_ADR_POS  + sizeof(((thr_header_t*)0)->src_adr))             //12
#define THR_CHN_ID_POS       (THR_ETH_TYP_POS  + sizeof(((thr_header_t*)0)->eth_type))            //14
#define THR_FRAG_NUM_POS     (THR_CHN_ID_POS   + sizeof(((thr_header_t*)0)->chn_id))              //15
#define THR_FRAG_TOT_NUM_POS (THR_FRAG_NUM_POS + sizeof(((thr_header_t*)0)->frag_num))            //17
#define THR_FRAG_LEN_POS     (THR_FRAG_TOT_NUM_POS + sizeof(((thr_header_t*)0)->frag_num_tot))    //19
#define THR_PAYLOAD_POS      (THR_FRAG_LEN_POS + sizeof(((thr_header_t*)0)->frag_len))            //21
#define THR_MSG_HEADER_LEN   THR_PAYLOAD_POS

#define THR_MSG_DST_ADR       (unsigned char*)(&thr_msg[THR_DST_ADR_POS])
#define THR_MSG_SRC_ADR       (unsigned char*)(&thr_msg[THR_SRC_ADR_POS])
#define THR_MSG_ETH_TYP       *(unsigned short*)(&thr_msg[THR_ETH_TYP_POS])
#define THR_MSG_CHN_ID        *(unsigned char*)(&thr_msg[THR_CHN_ID_POS])
#define THR_MSG_FRAG_NUM      *(unsigned short*)(&thr_msg[THR_FRAG_NUM_POS])
#define THR_MSG_FRAG_TOT_NUM  *(unsigned short*)(&thr_msg[THR_FRAG_TOT_NUM_POS])
#define THR_MSG_FRAG_LEN      *(unsigned short*)(&thr_msg[THR_FRAG_LEN_POS])
#define THR_MSG_PAYLOAD       (unsigned char*)(&thr_msg[THR_PAYLOAD_POS])


/* local type for the Throttle Channel structure */
struct thr_chn_t
{
	unsigned char dst_adr[ETH_ADR_LEN];/* destination MAC address                    */
	unsigned char id;                  /* Channel id                                 */
	unsigned short frag_size;          /* Fragment size                              */
	unsigned short freq;               /* Message transmission frequency             */
	thr_msg_handler_t funct;           /* Callback function invoked at the reception */
	unsigned short msg_length;
	unsigned short* p_msg;
};

/* Type for the Throttle message */
typedef struct
{
   unsigned char  dst_adr[ETH_ADR_LEN];  /* destination MAC address  */
   unsigned char  src_adr[ETH_ADR_LEN];  /* source MAC address       */
   unsigned short eth_type;              /* ethernet packet type     */
   unsigned char  chn_id;                /* channel identification   */
   unsigned short frag_num;              /* fragment number          */
   unsigned short frag_num_tot;          /* total fragment number    */
   unsigned short frag_len;              /* fragment length          */
}thr_header_t;

/* Local functions */
static int thr_msg_check(const unsigned char* thr_msg, unsigned short chn_id, unsigned short length);

static struct eth_int_t* eth_int = NULL;

/**
* @fn         int thr_init(const char* eth_int_name)
*
* @brief      Initializes the Throttle Net Driver, enabling the Ethernet Raw communication for the eth_int_name interface.
*
* @param [in] eth_int_name: ethernet interface name (e.g. "eth0","eth1")
*
* @retval     int: 0 on success; -1 on error and errno is set appropriately
*
*******************************************************************************/
int thr_init(const char* eth_int_name)
{
   int ret = 0;

   if (NULL == eth_int_name)
   {
      ret = -1;
      errno = EINVAL;
      DISPLAY_ERR("Input parameter NULL");
   }
   else
   {
      eth_int = eth_open(eth_int_name); /* open the Ethernet socket */
      if (NULL == eth_int)
      {
         ret = -1;
      }
   }
   return(ret);
}

/**
* @fn         struct thr_chn_t* thr_open_chn(const char* dst_adr, unsigned char chn_id, unsigned char frag_size, unsigned short freq, const thr_msg_handler_t funct);
*
* @brief      Open a logic channel
*
* @param [in] dst_adr: destination MAC address;
* @param [in] chn_id: identification of the channel;
* @param [in] frag_size: fragment size;
* @param [in] freq: frequency of trasmission for each fragment;
* @param [in] msg_handler: callback invoked when a message is completely received;
*
* @retval     struct thr_chn_t*: pointer to the Channel structure. NULL on error and errno is set appropriately.
*
*******************************************************************************/
struct thr_chn_t* thr_open_chn(const unsigned char* dst_adr, unsigned char chn_id, unsigned char frag_size, unsigned short freq, const thr_msg_handler_t funct)
{
   struct thr_chn_t* tmp_chn = NULL; /* pointer to Channel structure */

   /* TO-DO: check if a similar channel has already been created */

   /* Input parameters not valid ? */
   if ((NULL == dst_adr) || (0 == frag_size))
   {
      errno = EINVAL;
      DISPLAY_ERR("Input parameter(s) NULL");
   }
   else
   {
      /* allocate memory for the Channel structure */
      tmp_chn = (struct thr_chn_t*)malloc(sizeof(struct thr_chn_t));
      if (NULL == tmp_chn)
      {
         DISPLAY_ERR("Error allocating memory!");
      }
      else
      {
         /* Update the Channel structure */
         memcpy(tmp_chn->dst_adr, dst_adr, sizeof(tmp_chn->dst_adr));
         tmp_chn->id = chn_id;
         tmp_chn->frag_size = frag_size;
         tmp_chn->freq = freq;
         tmp_chn->funct = funct;
      }
      printf("thr_open_chn: callback = %x\n", tmp_chn->funct);

   }

   return(tmp_chn);
}


/**
* @fn         int thr_send(const struct thr_chn_t* thr_chn, const char* data, unsigned int length)
*
* @brief      Sends a Throttle message. A channel must be opened first.
*
* @param [in] thr_chn: pointer to the Throttle channel descriptor;
* @param [in] data: data buffer to be sent;
* @param [in] length: length of the data buffer;
*
* @retval     int: number of byte sent, on success. -1 on error and errno is set appropriately.
*
*******************************************************************************/
int thr_send(const struct thr_chn_t* thr_chn, const char* data, unsigned int length)
{
   unsigned char* thr_msg;
   unsigned char i;
   int ret = 0;

   /* Check the input parameters */
   if ((NULL == thr_chn) || (NULL == data) || (0 == length))
   {
      ret = -1;
      errno = EINVAL;
      DISPLAY_ERR("Input parameter(s) NULL");
   }
   else
   {
      if (0 == thr_chn->frag_size)
      {
         ret = -1;
         errno = ERANGE;
         DISPLAY_ERR("Division by zero");
      }
      else
      {
         /* allocate memory for the Throttle Message */
         thr_msg = (unsigned char*)malloc(THR_MSG_HEADER_LEN + thr_chn->frag_size);
         if (NULL == thr_msg)
         {
            ret = -1;
            DISPLAY_ERR("Error allocating memory!");
         }
         else
         {
            /* Compose the Ethernet Frame to be sent */
            memcpy(THR_MSG_DST_ADR, &thr_chn->dst_adr[0], ETH_ADR_LEN);     /* Destiantion MAC Address  */
            eth_getMACadr(eth_int, THR_MSG_SRC_ADR);                        /* Source MAC Address       */
            THR_MSG_ETH_TYP = htons(THROTTLENET_PROTO);                     /* Ethernet Packet Type     */
            THR_MSG_CHN_ID = thr_chn->id;                                   /* Channel identification   */
            THR_MSG_FRAG_TOT_NUM = ((length - 1) / thr_chn->frag_size) + 1; /* Total number of fragment */

            struct timespec thr_time;
            thr_time.tv_sec = thr_chn->freq / 1000;
            thr_time.tv_nsec = (thr_chn->freq % 1000) * 1000000;

            /* Message is splitted into fragments and they are sent */
            for (i = 1; i <= THR_MSG_FRAG_TOT_NUM; i++)
            {
               THR_MSG_FRAG_NUM = i; /* fragment number */
               /* update the fragment length */
               if (length >= thr_chn->frag_size)
               {
                  THR_MSG_FRAG_LEN = thr_chn->frag_size;
                  length -= thr_chn->frag_size;
               }
               else
               {
                  THR_MSG_FRAG_LEN = length;
               }
               memcpy ((void*)THR_MSG_PAYLOAD, (void*)data, THR_MSG_FRAG_LEN);                /* update the payload */
               ret = eth_send(eth_int, &thr_msg[0], (THR_MSG_HEADER_LEN + THR_MSG_FRAG_LEN)); /* send the message   */
               if (-1 == ret)  /*Error during the Ethernet trasmission ? */
               {
                  DISPLAY_ERR("Error during Throttle msg trasmission!");
                  break;
               }
               else
               {
                  data += THR_MSG_FRAG_LEN;
                  nanosleep(&thr_time, NULL);
               }
            }
            /*deallocate the memory */
            free(thr_msg);
            thr_msg = NULL;
         }
      }
   }
   return(ret);
}

int thr_receive(struct thr_chn_t* thr_chn, unsigned char* data, void* param)
{
   unsigned char* thr_msg = NULL;
   unsigned char* p_data = NULL;
   unsigned short frag_index = 1;
   short byte_rec = 0;
   unsigned char msg_received = 0;
   int ret;

   if ((NULL == thr_chn) || (NULL == data) || (NULL == thr_chn->funct) || (NULL == param))
   {
      ret = -1;
      errno = EINVAL;
      DISPLAY_ERR("Input parameter(s) NULL");
   }
   else
   {
      /* allocate memory for the Throttle Message */
      thr_msg = (unsigned char*)malloc(THR_MSG_HEADER_LEN + thr_chn->frag_size);
      if (NULL == thr_msg)
      {
         ret = -1;
         DISPLAY_ERR("Error allocating memory!");
      }
      else
      {
         p_data = data;
         do
         {
            byte_rec = eth_receive(eth_int, thr_msg, (THR_MSG_HEADER_LEN + thr_chn->frag_size));  /* receive the Ethernet Raw packet */
            if (-1 == byte_rec)
            {
               /* discard the message */
               DISPLAY_ERR("Error during Throttle msg reception: Fragment discarded.");
            }
            else
            {
               /* check if the Ethernet Raw message is correct (Ethernet protocol, Channel ID, length) */
               ret = thr_msg_check(thr_msg, thr_chn->id, byte_rec);
               if (ret < 0)
               {
                  //printf("Throttle msg mismatch: Fragment discarded.\n");
               }
               else
               {
                  printf("Message Index %d on %d. Actual Index %d\n", THR_MSG_FRAG_NUM, THR_MSG_FRAG_TOT_NUM, frag_index);
                  if (frag_index == THR_MSG_FRAG_NUM) /* The fragment is the one expected ? */
                  {
                     /* Rebuild the original data linking the payloads of each fragment */
                     memcpy((void*)p_data, (void*)THR_MSG_PAYLOAD, THR_MSG_FRAG_LEN);
                     p_data += THR_MSG_FRAG_LEN;  /* update the pointer to the buffer   */
                     frag_index++;                /* update the fragment index          */
                     ret = p_data - data;         /* update the number of received byte */
                     if (frag_index > THR_MSG_FRAG_TOT_NUM)
                     {
                        msg_received = 1;
                     }
                  }
                  else
                  {
                     printf("Fragment mismatch: Fragment discarded.\n");
                     frag_index = 1;
                     p_data = data;
                  }
               }
            }
         }while (msg_received != 1);

         thr_chn->p_msg = (unsigned char*)malloc(ret);
         if (NULL == thr_chn->p_msg)
         {
            ret = -1;
            DISPLAY_ERR("Error allocating memory!");
         }
         else
         {
            printf("Number of byte receive %d\n",ret);
            thr_chn->msg_length = ret;
            memcpy(thr_chn->p_msg, data, ret); /* copy the msg into the thr structure */
            printf("calling %x\n", thr_chn->funct);
            (thr_chn->funct)(param);
            free(thr_chn->p_msg);
            thr_chn->p_msg = NULL;
         }

         /* deallocate memory for the Throttle Message */
         free(thr_msg);
         thr_msg = NULL;
         p_data = NULL;
      }
   }
   return(ret);
}


static int thr_msg_check(const unsigned char* thr_msg, unsigned short chn_id, unsigned short length)
{
   int ret = 0;

   if (length > THR_MSG_HEADER_LEN)                    /* Ethernet Raw Packet contains a valid Payload ? */
   {
      if (THROTTLENET_PROTO == ntohs(THR_MSG_ETH_TYP)) /* Is Ethernet Type THROTTLENET Protocol ?        */
      {
         if (THR_MSG_CHN_ID == chn_id)                 /* Is Channel identification correct ?            */
         {
            /* throttle message is correct */
         }
         else
         {
            ret = -1;
         }
      }
      else
      {
         /* discard the message */
         ret = -2;
      }
   }
   else
   {
      /* discard the message */
      ret = -3;
   }
   return(ret);
}


int thr_read(struct thr_chn_t* thr_chn, unsigned char* data, int length)
{
   int ret = 0;

   if ((NULL == thr_chn) || (NULL == data))
   {
      ret = -1;
      errno = EINVAL;
      DISPLAY_ERR("Input parameter(s) NULL");
   }
   else
   {
      if (length > thr_chn->msg_length)
      {
         length = thr_chn->msg_length;
         printf("thr_read: truncating length to %d\n", length);
      }
      printf("thr_read: calling memcpy(%x, %x, %d\n", data, thr_chn->p_msg, length);
      memcpy(data, thr_chn->p_msg, length); /* copy the msg into the thr structure */
      ret = length;
      thr_chn->msg_length = 0;
   }
   return(ret);
}