Contiki 2.5
ihex.c
1 #include <unistd.h>
2 #include <strings.h>
3 #include <stdio.h>
4 #include <string.h>
5 
6 #include <inttypes.h>
7 
8 int hexfile_parse(char *line, unsigned int *type, unsigned int *addr, unsigned char *buffer)
9 {
10  unsigned int row_len = 0;
11  unsigned int row_index = 7;
12  unsigned int i;
13  int tmp;
14 
15  uint8_t cksum = 0;
16  int retval = 0;
17 
18  retval = sscanf(line, ":%2x%4x%2x", &row_len, addr, type);
19 
20  cksum += row_len;
21  cksum += *addr >> 8;
22  cksum += *addr & 0xFF;
23  cksum += *type;
24 
25  i = 0;
26  if (retval == 3)
27  {
28  while(i < row_len)
29  {
30 
31  if (sscanf(&line[row_index], "%2x", &tmp) == 1)
32  {
33  cksum += tmp;
34  buffer[i++] = (unsigned char) tmp;
35  row_index += 2;
36  }
37  else return -1;
38  }
39  if (sscanf(&line[row_index], "%2x", &tmp) == 1)
40  {
41  if ((cksum + (uint8_t) tmp) == 0) return row_len;
42  }
43  }
44  return -1;
45 }
46 
47 int hexfile_out(char *line, unsigned int type, unsigned int address, unsigned char *data, unsigned int bytes)
48 {
49  uint8_t cksum = 0;
50  uint8_t i = 0;
51  char tmp[8];
52 
53  sprintf(line, ":%2.2X%4.4X%2.2X", bytes, address, type);
54  cksum -= bytes;
55  cksum -= address >> 8;
56  cksum -= address & 0xFF;
57  cksum -= type;
58 
59  for (i=0; i<bytes; i++)
60  {
61  sprintf(tmp, "%2.2X", data[i]);
62  strcat(line, tmp);
63  cksum -= data[i];
64  }
65  sprintf(tmp, "%2.2X\r\n", cksum);
66  strcat(line, tmp);
67 
68  return strlen(line);
69 }
70