@@ -124,29 +124,32 @@ int prepare_fileWrite(GANGLIONOBJ * st) {
124124}
125125
126126
127- int get_integers (int * buf, char * str)
127+ int get_integers (int * buf, char * str, int max )
128128{
129- int i=0 ,actval=0 ,sign=1 ;
129+ int i=0 ,actval=0 ,sign=1 ,valid= 0 ;
130130
131- while ((*str) && (*str!= ' ; ' ) && (i< 10 )) {
131+ while ((*str)&&(i<max )) {
132132 if ((*str>=' 0' ) && (*str<=' 9' ))
133133 {
134134 actval=actval*10 ;
135135 actval=actval+(*str-' 0' );
136+ valid=1 ;
136137 }
137- else if (*str==' ,' ) {
138- buf[i]=actval*sign;
139- // printf("integer %d received: %d\n",i,buf[i]);
140- i++; actval=0 ; sign=1 ;
138+ else if ((*str==' -' ) && (*(str+1 )>=' 0' ) && (*(str+1 )<=' 9' )) {
139+ sign=-1 ;
140+ valid=1 ;
141141 }
142- else if (*str== ' ] ' ) {
142+ else if (valid ) {
143143 buf[i]=actval*sign;
144+ i++; actval=0 ; sign=1 ; valid=0 ;
144145 // printf("integer %d received: %d\n",i,buf[i]);
145- i++; break ;
146146 }
147- else if (*str==' -' ) sign=-1 ;
148147 str++;
149148 }
149+ if (valid && (i<max)) {
150+ buf[i]=actval*sign;
151+ i++;
152+ }
150153 return (i);
151154}
152155
@@ -178,7 +181,7 @@ DWORD WINAPI TcpReaderProc(LPVOID lpv)
178181{
179182 int len=0 ,cnt;
180183 char tmpstr[500 ];
181- char *c, *actline;
184+ char *c, *actline, *t ;
182185 bool reading=true ;
183186 printf (" Ganglion Reader Thread running!\n " );
184187
@@ -195,31 +198,32 @@ DWORD WINAPI TcpReaderProc(LPVOID lpv)
195198 if ((len = SDLNet_TCP_Recv (sock, readbuf, sizeof (readbuf))) > 0 )
196199 {
197200 readbuf[len] = ' \0 ' ;
198- // printf("TCP received :%s\n",readbuf);
201+ // printf("Received :%s\n",readbuf);
199202 actline=readbuf;
200- while ((actline!= NULL ) && (strlen (actline)> 5 )) {
203+ while ((actline) && (strlen (actline))) {
201204 // if (strstr(actline,"t,204,")==actline) {
202- if (strstr (actline," {\" startByte" )==actline) {
203- // we received a packet with new channel values !
204- c=actline+56 ;
205- cnt=get_integers (intbuffer,c);
205+ if (t=strstr (actline," {\" startByte" )) {
206+ // we received a packet with channel values!
207+ c=strstr (t," [" ); // beginning of channel values
208+ cnt=get_integers (intbuffer,c,4 );
209+ // printf("Extract:");
210+ // for (int z=0;z<4;z++) printf ("%d, ",intbuffer[z]);
211+ // printf("\n");
206212 state=STATE_READING;
207213 process_packets (); // this triggers all signal processing !
208214 }
209215 else if (strstr (actline," \" code\" :412" ) || strstr (actline," \" code\" :501" )) {
210- // printf("received: %s\n",actline);
211216 printf (" Ganglion connection error - is the correct BT-dongle connected ?\n " );
212217 state=STATE_IDLE;
213218 MessageBox (NULL ," Is the correct BT-dongle connected ?" , " ganglion connection error" , MB_OK|MB_TOPMOST);
214219 }
215220 else if (strstr (actline," \" code\" :400" ) || strstr (actline," \" code\" :401" ) || strstr (actline," \" code\" :413" )) {
216- // printf("received: %s\n",actline);
217221 printf (" Ganglion connection error - is the Ganglion board switched on and connected ?\n " );
218222 state=STATE_IDLE;
219223 MessageBox (NULL ," Is the Ganglion board switched on and connected?" , " ganglion connection error" , MB_OK|MB_TOPMOST);
220224 }
221- else if (strstr (actline," {\" name\" :\" Ganglion-" )==actline ) {
222- strcpy (tmpstr,actline +9 );
225+ else if (t= strstr (actline," {\" name\" :\" Ganglion-" )) {
226+ strcpy (tmpstr,t +9 );
223227 if ((c=strstr (tmpstr," \" " ))) *c=0 ;
224228 state=STATE_SCANNING;
225229 int found=0 ;
@@ -239,23 +243,22 @@ DWORD WINAPI TcpReaderProc(LPVOID lpv)
239243 printf (" active devices:%d\n " ,num_ganglions);
240244 }
241245 }
242- else if (( strstr (actline," {\" channelNumber\" " )==actline) && ( strlen (actline)> 50 )) {
246+ else if (t= strstr (actline," {\" channelNumber\" " )) {
243247 state=STATE_GETIMPEDANCE;
244- c=actline+17 ;
245- cnt=get_integers (intbuffer,c);
248+ cnt=get_integers (intbuffer,t+17 ,2 );
246249 if (intbuffer[0 ]>0 ) {
247250 printf (" Found impedance channel %d: %d\n " ,intbuffer[0 ],intbuffer[1 ]);
248251 update_impedances (intbuffer[0 ]-1 ,intbuffer[1 ]/2 );
249252 }
250253 }
251- else if (strstr (actline," {\" code\" :200,\" type\" :\" connect\" }" )==actline ) {
254+ else if (t= strstr (actline," {\" code\" :200,\" type\" :\" connect\" }" )) {
252255 state=STATE_CONNECTED;
253256 printf (" Connected state detected!\n " );
254257 }
255- else printf (" received: %s" ,readbuf);
258+ else if ( strlen (actline) > 5 ) printf (" Unprocessed Hub message: %s" ,readbuf);
256259
257- actline=strstr (actline," \n " );
258- if (( actline!= NULL ) && ( strlen ( actline)> 5 )) actline+= 1 ; // skip newline, go to next line
260+ actline=strstr (actline," } " );
261+ if (actline) actline++ ; // continue parsing
259262 }
260263 } else { printf (" read returned zero, closing reader thread\n " ); return (-1 ); }
261264 }
0 commit comments