28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
43 cJSON *root, *json_jsonrpc;
45 cJSON *json_error, *json_code, *json_message;
51 printf(
"no bytes read. error !!\n");
56 if (length > strlen(buffer))
58 printf(
"Invalid value for parameter length %d ", len );
62 json = strstr(buffer,
"\r\n\r\n");
73 printf(
"Response message:\n%d:%s\n",length,json);
78 root = cJSON_Parse(json);
82 json_jsonrpc = cJSON_GetObjectItem(root,
"jsonrpc");
84 cJSON_Print(json_jsonrpc);
92 json_error = cJSON_GetObjectItem(root,
"error");
95 json_code = cJSON_GetObjectItem(json_error,
"code");
97 cJSON_Print(json_code);
100 json_message = cJSON_GetObjectItem(json_error,
"message");
102 cJSON_Print(json_message);
105 printf(
"Received FAILURE response,%s(%d), from the server.\n",json_message->valuestring, json_code->valueint);
109 json_result = cJSON_GetObjectItem(root,
"result");
116 cJSON_Print(json_result);
119 printf(
"Result:%d.\n", json_result->valueint);
126 cJSON *json_btm, *json_ebt, *json_es;
128 json_ebt = cJSON_GetObjectItem(json_result,
"enable-buffer-tracking");
130 cJSON_Print(json_ebt);
133 json_btm = cJSON_GetObjectItem(json_result,
"buffer-tracking-mode");
135 cJSON_Print(json_btm);
138 json_es = cJSON_GetObjectItem(json_result,
"enable-snapshots");
140 cJSON_Print(json_es);
143 printf(
"enable-buffer-tracking:%d\nbuffer-tracking-mode:%s\nenable-snapshots:%d.\n", json_ebt->valueint, json_btm->valuestring,json_es->valueint);
152 json_gport = cJSON_GetObjectItem(json_result,
"global-port-id");
154 cJSON_Print(json_gport);
157 printf(
"gport:%d.\n", json_gport->valueint);
164 cJSON *json_device, *json_revision;
166 json_device = cJSON_GetObjectItem(json_result,
"device");
168 cJSON_Print(json_device);
171 json_revision = cJSON_GetObjectItem(json_result,
"revision");
173 cJSON_Print(json_revision);
176 printf(
"Device:%d Revision:%d.\n", json_device->valueint, json_revision->valueint);
183 cJSON *json_maxunits;
185 json_maxunits = cJSON_GetObjectItem(json_result,
"max-units");
187 cJSON_Print(json_maxunits);
190 printf(
"Max-Units:%d.\n", json_maxunits->valueint);
196 cJSON *json_ge, *json_xe, *json_ce, *json_port, *json_cpu, *json_all;
198 json_ge = cJSON_GetObjectItem(json_result,
"ge-bmp");
200 cJSON_Print(json_ge);
203 json_xe = cJSON_GetObjectItem(json_result,
"xe-bmp");
205 cJSON_Print(json_xe);
208 json_ce = cJSON_GetObjectItem(json_result,
"ce-bmp");
210 cJSON_Print(json_ce);
213 json_port = cJSON_GetObjectItem(json_result,
"port-bmp");
215 cJSON_Print(json_port);
218 json_cpu = cJSON_GetObjectItem(json_result,
"cpu-bmp");
220 cJSON_Print(json_cpu);
223 json_all = cJSON_GetObjectItem(json_result,
"all-bmp");
225 cJSON_Print(json_all);
255 int main(
int argc,
char *argv[]){
257 int temp = 0, choice;
258 struct sockaddr_in serverAddr;
260 char server[128] = {0};
261 char buffer[1024] = {0};
265 clientSocket = socket(PF_INET, SOCK_STREAM, 0);
266 if (clientSocket == -1)
268 printf(
"failed to create socket \n");
274 serverAddr.sin_family = AF_INET;
283 strncpy(server, argv[1],(
sizeof(server) - 1));
293 serverAddr.sin_addr.s_addr = inet_addr(server);
295 memset(serverAddr.sin_zero,
'\0',
sizeof serverAddr.sin_zero);
298 addr_size =
sizeof serverAddr;
299 temp = connect(clientSocket, (
struct sockaddr *) &serverAddr, addr_size);
302 printf(
"failed to connect \n");
309 printf(
"\r\nUser menu: Select one of the following JSON-RPC APIs\r\n");
310 printf(
"1. configure-buffer-tracking\n");
311 printf(
"2. get-buffer-tracking\n");
312 printf(
"3. get-global-portid\n");
313 printf(
"4. get-unit-info\n");
314 printf(
"5. get-max-units\n");
315 printf(
"6. get-port-config\n");
316 printf(
"7. get-gportid\n");
317 printf(
"0. Quit the application\n");
321 printf(
"Invalid option entered. Please re-enter.\n");
329 char *json_api_template =
"{\
330 \"jsonrpc\" : \"2.0\",\
331 \"method\" : \"configure-buffer-tracking\",\
334 \"enable-buffer-tracking\" : 1,\
335 \"buffer-tracking-mode\" : \"peak\",\
336 \"enable-snapshots\" : 1\
340 strncpy (buffer, json_api_template, 1023);
346 char *json_api_template =
"{\
347 \"jsonrpc\" : \"2.0\",\
348 \"method\" : \"get-buffer-tracking\",\
353 strncpy (buffer, json_api_template, 1023);
360 char *json_api_template =
"{\
361 \"jsonrpc\" : \"2.0\",\
362 \"method\" : \"get-global-portid\",\
369 strncpy (buffer, json_api_template, 1023);
376 char *json_api_template =
"{\
377 \"jsonrpc\" : \"2.0\",\
378 \"method\" : \"get-unit-info\",\
383 strncpy (buffer, json_api_template, 1023);
390 char *json_api_template =
"{\
391 \"jsonrpc\" : \"2.0\",\
392 \"method\" : \"get-max-units\",\
397 strncpy (buffer, json_api_template, 1023);
404 char *json_api_template =
"{\
405 \"jsonrpc\" : \"2.0\",\
406 \"method\" : \"get-port-config\",\
411 strncpy (buffer, json_api_template, 1023);
418 char *json_api_template =
"{\
419 \"jsonrpc\" : \"2.0\",\
420 \"method\" : \"get-gportid\",\
427 strncpy (buffer, json_api_template, 1023);
434 printf(
"Exiting the application.\n");
443 temp = send(clientSocket, buffer, strlen(buffer), 0);
446 printf(
"failed to send any data \n");
450 memset(buffer, 0,
sizeof(buffer));
456 printf(
"Error reading from the socket.\n");
461 printf(
"End of file reached.\n");
471 printf(
"Error processing the response.\n");