ITS
api.cpp
Go to the documentation of this file.
1 #include <api.h>
2 
3 void empty_buffer(FILE* stream)
4 {
5  int c = 0;
6 
7  while (c != '\n' && c != EOF)
8  c = fgetc(stream);
9 }
10 
11 bool read_stdin(char *msg, int length, FILE *stream)
12 {
13 
14  char *returnPosition = NULL;
15 
16  if(fgets(msg, length, stream) != NULL)
17  {
18 
19  returnPosition = strchr(msg, '\n');
20 
21  if(returnPosition != NULL)
22  *returnPosition = '\0';
23  else
24  empty_buffer(stream);
25  return true;
26  }
27  else
28  {
29  empty_buffer(stream);
30  return false;
31  }
32 
33 }
34 
35 void format(unsigned char * msg, unsigned long v, int l)
36 {
37 
38  int i;
39 
40  for(i = l ; i ; i--){
41  msg[l - i] = v >> (8 * (i - 1));
42  v -= (v >> (8 * (i - 1))) << (8 * (i - 1));
43  }
44 
45 }
46 
47 unsigned long long int split(const unsigned char *msg, int begin, int end)
48 {
49  unsigned long long int ret;
50  int i, offset_b = 8 - (begin % 8), offset_e = end % 8, and_b = (1 << offset_b) - 1,
51  and_e = ((1 << offset_e) - 1) << (8 - offset_e), l = (end - begin + 7) / 8,
52  start = begin / 8;
53 
54  ret = msg[start] & and_b;
55 
56  for(i = 1 ; i < l ; i++)
57  {
58  ret <<= 8;
59  ret += msg[i + start] & and_b;
60  }
61 
62  ret <<= offset_e;
63  ret += msg[i + start] & and_e;
64 
65  return ret;
66 }
67 
68 void split(const unsigned char *msg, int begin, int end, char *out)
69 {
70  int i;
71 
72  for(i = begin ; i < end ; i++)
73  out[i - begin] = msg[i];
74 }
75 
76 void read_opts(const unsigned char *msg, int nOptions, bool *opts)
77 {
78  int i;
79 
80  for(i = 0 ; i < nOptions ; i++)
81  opts[i] = msg[(i / 8) + 1] & (1 << (7 - (i % 8)));
82 }
83 
84 void clear_string(char *s, int n)
85 {
86  memset(s, '\0', n);
87 }
88 
89 void print_hexa(const unsigned char *msg, int l, FILE *stream)
90 {
91 
92  int i;
93 
94  for(i = 0 ; i < l ; i++){
95  if(msg[i] < 0x10)
96  fprintf(stream, "0%X", msg[i]);
97  else
98  fprintf(stream, "%X", msg[i]);
99  if(!((i + 1) % OCTET))
100  fprintf(stream, " ");
101  if(!((i + 1) % NEW_LINE))
102  fprintf(stream, "\n");
103  }
104  printf("\n");
105 }
106 
107 QByteArray hexa2bin(QByteArray src)
108 {
109  QByteArray ret, hexByte;
110  QDataStream ds(&ret, QIODevice::WriteOnly);
111  quint8 hex;
112  int i, j;
113 
114  for(i = 0 ; i < src.size() ; i += 2)
115  {
116  hex = 0;
117  hexByte.clear();
118  for(j = 0 ; j < 2 ; j++)
119  hexByte.append(src[i + j]);
120  hex = hexByte.toInt(NULL, 16);
121  ds << hex;
122  }
123 
124  return ret;
125 }
126 
127 QByteArray split(QByteArray src, int begin, int end)
128 {
129  QByteArray ret;
130  int i, n = end - begin;
131 
132  for(i = 0 ; i < n ; i++)
133  ret += src[i + begin];
134 
135  return ret;
136 }
137 /*
138 struct gpsPosition_t getPosition()
139 {
140  struct gpsPosition_t p;
141  QFile file(POSITION_FILE);
142  QByteArray line;
143  QList<QByteArray> splitLine;
144 
145  if(file.open(QIODevice::ReadOnly))
146  {
147  while(!file.atEnd())
148  {
149  line = file.readLine();
150  splitLine = line.split(':');
151  if(splitLine[0] == QByteArray(GPS_CONF_LATITUDE))
152  p._latitude = splitLine[1].toInt();
153  else if(splitLine[0] == QByteArray(GPS_CONF_LONGITUDE))
154  p._longitude = splitLine[1].toInt();
155  else if(splitLine[0] == QByteArray(GPS_CONF_ALTITUDE))
156  p._altitude = splitLine[1].toInt();
157  else if(splitLine[0] == QByteArray(GPS_CONF_SEMI_MAJOR_CONFIDENCE))
158  p._semiMajorConfidence = splitLine[1].toUInt();
159  else if(splitLine[0] == QByteArray(GPS_CONF_SEMI_MINOR_CONFIDENCE))
160  p._semiMinorConfidence = splitLine[1].toUInt();
161  else if(splitLine[0] == QByteArray(GPS_CONF_SEMI_MAJOR_ORIENTATION))
162  p._semiMajorOrientation = splitLine[1].toUInt();
163  else if(splitLine[0] == QByteArray(GPS_CONF_TIMESTAMP))
164  p._timestamp = splitLine[1].toULongLong();
165  else if(splitLine[0] == QByteArray(GPS_CONF_SPEED))
166  p._speed = splitLine[1].toUInt();
167  }
168  }
169  else
170  memset(&p, DEFAULT_VALUE_POSITION, sizeof p);
171 
172  return p;
173 }
174 */
176 {
177  QString s = "{";
178  int i;
179  for(i=0; i<((8*b.size) - b.bits_unused) - 1; i++)
180  (b.buf[i/8] & (1 << (((((i/8)+1)*8)-1)-i)))? s += "true, ":s += "false, ";
181  (b.buf[i/8] & (1 << (((((i/8)+1)*8)-1)-i)))? s += "true":s += "false";
182  s += "}";
183  return s;
184 }
185 
186 int validate_unsigned_int(cfg_t *cfg, cfg_opt_t *opt)
187 {
188  long value = cfg_opt_getnint(opt, cfg_opt_size(opt) - 1);
189  if(value < 0)
190  {
191  cfg_error(cfg, "integer option '%s' must be positive in section '%s'",
192  opt->name, cfg->name);
193  return -1;
194  }
195  return 0;
196 }
197 
198 BIT_STRING_t makeBitString(int nbits, cfg_t* cfg, const char* opt_name)
199 {
200  BIT_STRING_t bs;
201  bs.size = (nbits + 7) / 8;
202  bs.bits_unused = (8 - (nbits % 8)) % 8;
203  bs.buf = (u_int8_t*) calloc(bs.size, 1);
204  for(int i=0; i<nbits; i++)
205  {
206  bs.buf[i/8] |= cfg_getnbool(cfg, opt_name, i) << (7 - (i % 8));
207  }
208  return bs;
209 }
210 
211 quint64 circAtLat(long l)
212 {
213  int L = l / 10000000;
214 
215  quint64 ret = 2 * M_PI * (cos(L / (180 / M_PI)) * EARTH_RADIUS);
216 
217  return ret;
218 }
219 
220 unsigned int diameter(long begin, long end, quint64 circ)
221 {
222  unsigned int tmp = abs(end - begin);
223  //tmp *= tmp < 0 ? -1 : 1;
224  tmp *= circ / 360;
225  return tmp;
226 }
227 
228 double dist(qint32 x1, qint32 y1, qint32 x2, qint32 y2)
229 {
230  //double a = pow((x2 - x1), 2);
231  //double b = pow((y2 - y1), 2);
232  //double res = sqrt(a+b);
233  return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
234 }
235 
236 double f(long x, long y, double lengthA, double lengthB, e_shape shape)
237 {
238  long longueur;
239  long largeur;
240 
241  switch((int)shape)
242  {
243  case shape_circle:
244  if(lengthA != 0)
245  return 1-pow((x/lengthA),2)-pow((y/lengthA),2);
246  else
247  return -1;
248  break;
249  case shape_rectangle:
250  if(lengthA != 0 && lengthB != 0)
251  {
252  longueur = 1-pow((x/lengthA),2);
253  largeur = 1-pow((y/lengthB),2);
254  return longueur<largeur?longueur:largeur;
255  }
256  else
257  return -1;
258  break;
259  case shape_ellipse:
260  if(lengthA != 0 && lengthB != 0)
261  return 1-((x/lengthA)*(x/lengthA))-((y/lengthB)*(y/lengthB));
262  else
263  return -1;
264  break;
265  case shape_none:
266  default:
267  return -1;
268  break;
269  }
270 }
271 
272 int g(double dist_r, double dist_f, double dist_fr, int angleThreshold)
273 {
274  double FSR = acos((pow(dist_r, 2) + pow(dist_f, 2) - pow(dist_fr, 2)) / (2*dist_r*dist_f));
275  if((dist_r < dist_r) && (dist_f < itsGnDefaultMaxCommunicationRange) && (FSR < angleThreshold))
276  return 1;
277  else
278  return -1;
279 }
Usefull functions and declarations.
void print_hexa(const unsigned char *msg, int l, FILE *stream)
print_hexa Print a byte array.
Definition: api.cpp:89
#define NEW_LINE
Definition: api.h:25
int validate_unsigned_int(cfg_t *cfg, cfg_opt_t *opt)
validate_unsigned_int Check if an option is an unsigned int
Definition: api.cpp:186
void read_opts(const unsigned char *msg, int nOptions, bool *opts)
Definition: api.cpp:76
unsigned long long int split(const unsigned char *msg, int begin, int end)
split Extract number from byte array.
Definition: api.cpp:47
QByteArray hexa2bin(QByteArray src)
Definition: api.cpp:107
QString printBitString(BIT_STRING_t b)
printBitString Print a BIT_STRING into a QString.
Definition: api.cpp:175
#define EARTH_RADIUS
Definition: api.h:22
int g(double dist_r, double dist_f, double dist_fr, int angleThreshold)
g Compute the g function.
Definition: api.cpp:272
unsigned int diameter(long begin, long end, quint64 circ)
diameter Compute the distance between two point on the same latitude.
Definition: api.cpp:220
#define OCTET
Definition: api.h:26
double dist(qint32 x1, qint32 y1, qint32 x2, qint32 y2)
dist This function calculate the euclidian distance between two points ([x1, y1], [x2...
Definition: api.cpp:228
double f(long x, long y, double lengthA, double lengthB, e_shape shape)
f Compute the f function.
Definition: api.cpp:236
void empty_buffer(FILE *stream)
Definition: api.cpp:3
void clear_string(char *s, int n)
Definition: api.cpp:84
e_shape
The e_shape enum defines the shapes type.
Definition: constantes.h:54
uint8_t * buf
Definition: BIT_STRING.h:15
bool read_stdin(char *msg, int length, FILE *stream)
Definition: api.cpp:11
int bits_unused
Definition: BIT_STRING.h:18
quint64 circAtLat(long l)
circAtLat Compute the circumference at a latitude.
Definition: api.cpp:211
#define itsGnDefaultMaxCommunicationRange
Definition: constantes.h:121
BIT_STRING_t makeBitString(int nbits, cfg_t *cfg, const char *opt_name)
makeBitString Generate a BIT_STRING from configuration.
Definition: api.cpp:198
void format(unsigned char *msg, unsigned long v, int l)
format Format a number in byte array.
Definition: api.cpp:35