blob: d919ba312aca37f491ba9021d71955782a99afb0 [file] [log] [blame]
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +05301/*
2** Copyright 2017-present Open Networking Foundation
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053016#include <signal.h>
17#include <stdio.h>
18#include <unistd.h>
19#include <sys/time.h>
20#include "bal_msg_type.grpc-c.h"
21#include "bal_osmsg.grpc-c.h"
22#include "bal_model_ids.grpc-c.h"
23#include "bal_obj.grpc-c.h"
24#include "bal_model_types.grpc-c.h"
25#include "bal_errno.grpc-c.h"
26#include "bal.grpc-c.h"
27
28#ifdef BAL_STUB
29#include "bal_stub.h"
30#else
31#include "asfvolt16_driver.h"
32#endif
33
34/* Global varibles */
35balCoreIpInfo coreIpPortInfo;
36
37static grpc_c_server_t *test_server;
38
39static void sigint_handler (int x) {
40 grpc_c_server_destroy(test_server);
41 exit(0);
42}
43
44/*
45 * This functions gets invoked whenever bal RPC gets called
46 */
47void bal__bal_cfg_get_cb(grpc_c_context_t *context)
48{
49 BalCfg *get_cfg;
50
51 /*
52 * Read incoming message into get_cfg
53 */
54 if (context->gcc_payload) {
55 context->gcc_stream->read(context, (void **)&get_cfg, 0);
56 }
57
58#ifndef BAL_STUB
59 //asfvolt16_bal_cfg_get(key, get_cfg);
60#endif
61}
62
63/*
64 * This functions gets invoked whenever bal RPC gets called
65 */
66void bal__bal_cfg_set_cb(grpc_c_context_t *context)
67{
68 BalCfg *set_cfg;
69 BalErr bal_err;
70 int ret_val = 0;
71
72 /*
73 * Read incoming message into set_cfg
74 */
75 if (context->gcc_payload) {
76 context->gcc_stream->read(context, (void **)&set_cfg, 0);
77 }
78
79 /*
80 * send it to BAL
81 */
82
83 bal_err__init(&bal_err);
84
85 bal_err.err= 0;
86
87 /*
88 * Write reply back to the client
89 */
90
91 ret_val = context->gcc_stream->write(context, &bal_err, 0);
92 if (ret_val != GRPC_C_WRITE_OK) {
93 if(ret_val == GRPC_C_WRITE_PENDING) {
A R Karthick29c16212017-09-05 17:25:35 -070094 printf("write is pending, sleep for 10 sec: %d\n", ret_val);
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053095 sleep(10);
96 }
97 else {
A R Karthick29c16212017-09-05 17:25:35 -070098 printf("Failed to write: %d\n", ret_val);
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +053099 exit(1);
100 }
101 }
102
103 grpc_c_status_t status;
104 status.gcs_code = 0;
105
106 /*
107 * Finish response for RPC
108 */
109 if (context->gcc_stream->finish(context, &status)) {
110 printf("Failed to write status\n");
111 exit(1);
112 }
113
114#ifdef BAL_STUB
115 pthread_mutex_lock(&lock);
116
117 struct QNode *temp = newNode(set_cfg->hdr->obj_type,
118 BAL_ERRNO__BAL_ERR_OK,
119 set_cfg->device_id);
120
121/* if(set_cfg->hdr->has_obj_type)
122 { */
123
124 switch(set_cfg->hdr->obj_type)
125 {
126 case BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL:
127 {
128 printf("\n***************************************************\n");
129 printf("Received Access Terminal Configuration msg\n");
130 printf("***************************************************\n");
131 }
132 break;
133 case BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE:
134 {
135 printf("\n***************************************************\n");
136 printf("Received PON Interface Configuration msg\n");
137 printf("***************************************************\n");
138 temp->intf_id = set_cfg->interface->key->intf_id;
139 printf("Pon ID = %d\n", temp->intf_id);
140 }
141 break;
142 case BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL:
143 {
144 printf("\n*****************************************************\n");
145 printf("Received ONU Activation msg\n");
146 printf("*****************************************************\n");
147 temp->intf_id = set_cfg->terminal->key->intf_id;
148 temp->onu_id = set_cfg->terminal->key->sub_term_id;
149 memset(temp->vendor_id, 0, BAL_DEVICE_STR_LEN);
150 memcpy(temp->vendor_id,
151 set_cfg->terminal->data->serial_number->vendor_id,
152 strlen(set_cfg->terminal->data->serial_number->vendor_id));
153 memset(temp->vendor_specific, 0, BAL_DEVICE_STR_LEN);
154 memcpy(temp->vendor_specific,
155 set_cfg->terminal->data->serial_number->vendor_specific,
156 strlen(set_cfg->terminal->data->serial_number->vendor_specific));
157
158 }
159 break;
160 case BAL_OBJ_ID__BAL_OBJ_ID_PACKET:
161 {
162 switch(set_cfg->packet->key->packet_send_dest->type)
163 {
164 case BAL_DEST_TYPE__BAL_DEST_TYPE_ITU_OMCI_CHANNEL:
165 {
166 printf("\n*****************************************************\n");
167 printf("Received OMCI msg\n");
168 printf("*****************************************************\n");
169 temp->intf_id = set_cfg->terminal->key->intf_id;
170 temp->onu_id = set_cfg->terminal->key->sub_term_id;
171 }
172 break;
173 default:
174 {
175 ("\n*****************************************************\n");
176 printf("Dest type invalid\n");
177 printf("*****************************************************\n");
178 }
179 break;
180 }
181 }
182 break;
183 default:
184 {
185 ("\n*****************************************************\n");
186 printf("Received Invalid msg\n");
187 printf("*****************************************************\n");
188 pthread_mutex_unlock(&lock);
189 return;
190 }
191 break;
192 }
193 enQueue(set_cfg->hdr->obj_type, temp);
194/*
195 }
196 else
197 {
198 printf("BALSTUB:Cfg Set recevied without object type");
199 } */
200 pthread_mutex_unlock(&lock);
201 sleep(2);
202 pthread_cond_signal(&cv);
203/*
204 if(BAL_OBJ_ID__BAL_OBJ_ID_INTERFACE == set_cfg->hdr->obj_type)
205 {
206 sleep(5);
207 struct QNode *temp1 = newNode(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL,
208 BAL_ERRNO__BAL_ERR_OK,
209 set_cfg->device_id);
210 temp1->intf_id = set_cfg->interface->key->intf_id;
211 temp1->onu_id = 65535;
212 printf("sending _onu_discovery_indiaction\n");
213 enQueue(BAL_OBJ_ID__BAL_OBJ_ID_SUBSCRIBER_TERMINAL, temp1);
214 pthread_cond_signal(&cv);
215 }
216*/
217#else
218 if(BAL_OBJ_ID__BAL_OBJ_ID_ACCESS_TERMINAL == set_cfg->hdr->obj_type)
219 {
220 sleep(5);//enable this if running with gdb
221 }
222 asfvolt16_bal_cfg_set(set_cfg);
223#endif
224}
225
226
227/*
228 * This functions gets invoked whenever bal clear RPC gets called
229 */
230void bal__bal_cfg_clear_cb(grpc_c_context_t *context)
231{
232 BalKey *clear_key;
233
234 /*
235 * Read incoming message into clear_key
236 */
237 if (context->gcc_payload) {
238 context->gcc_stream->read(context, (void **)&clear_key, 0);
239 }
240
241#ifndef BAL_STUB
242 asfvolt16_bal_cfg_clear(clear_key);
243#endif
244}
245
246
247/*
248 * This functions gets invoked whenever bal Init RPC gets called
249 */
250void bal__bal_api_init_cb(grpc_c_context_t *context)
251{
252 BalInit *bal_init;
253 BalErr bal_err;
254 int ret_val;
255
256 /*
257 * Read incoming message into set_cfg
258 */
259 if (context->gcc_payload) {
260 context->gcc_stream->read(context, (void **)&bal_init, 0);
261 }
262
263 /*
264 * send it to BAL
265 */
266
267
268 ("\n*****************************************************\n");
269 printf("Received API Init msg\n");
270 printf("*****************************************************\n");
271
272 bal_err__init(&bal_err);
273
274 bal_err.err= 0;
275
276 /*
277 * Write reply back to the client
278 */
279 ret_val = context->gcc_stream->write(context, &bal_err, 0);
280 if (ret_val != GRPC_C_WRITE_OK) {
281 if(ret_val == GRPC_C_WRITE_PENDING) {
A R Karthick29c16212017-09-05 17:25:35 -0700282 printf("write is pending, sleep for 10 sec: %d\n", ret_val);
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530283 sleep(10);
284 }
285 else {
A R Karthick29c16212017-09-05 17:25:35 -0700286 printf("Failed to write: %d\n", ret_val);
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530287 exit(1);
288 }
289 }
290
291 grpc_c_status_t status;
292 status.gcs_code = 0;
293
294 /*
295 * Finish response for RPC
296 */
297 if (context->gcc_stream->finish(context, &status)) {
298 printf("Failed to write status\n");
299 exit(1);
300 }
301
302#ifndef BAL_STUB
303 asfvolt16_bal_init(bal_init, &coreIpPortInfo);
304#else
305 printf("\nRecevied IP Address == %s \n", bal_init->voltha_adapter_ip_port);
306 stub_bal_init(bal_init);
307#endif
308
309}
310
311
312/*
313 * This functions gets invoked whenever bal finish RPC gets called
314 */
315void bal__bal_api_finish_cb(grpc_c_context_t *context)
316{
317#if 0
318 void *finish_init;
319
320 /*
321 * Read incoming message into set_cfg
322 */
323 if (context->gcc_payload) {
324 context->gcc_stream->read(context, (void **)&finish_init);
325 }
326#endif
327}
328
329#if 0
330/*
331 * This functions gets invoked whenever bal finish RPC gets called
332 */
333void bal_ind__bal_ind_info_cb(grpc_c_context_t *context)
334{
335#if 0
336 void *finish_init;
337
338 /*
339 * Read incoming message into set_cfg
340 */
341 if (context->gcc_payload) {
342 context->gcc_stream->read(context, (void **)&finish_init);
343 }
344#endif
345}
346#endif
347
348void bal_ind__bal_acc_term_ind_cb(grpc_c_context_t *context)
349{
350}
351void bal_ind__bal_acc_term_oper_sts_cng_ind_cb(grpc_c_context_t *context)
352{
353}
354void bal_ind__bal_flow_oper_sts_cng_cb(grpc_c_context_t *context)
355{
356}
357void bal_ind__bal_flow_ind_cb(grpc_c_context_t *context)
358{
359}
360void bal_ind__bal_group_ind_cb(grpc_c_context_t *context)
361{
362}
363void bal_ind__bal_iface_oper_sts_cng_cb(grpc_c_context_t *context)
364{
365}
366void bal_ind__bal_iface_los_cb(grpc_c_context_t *context)
367{
368}
369void bal_ind__bal_iface_ind_cb(grpc_c_context_t *context)
370{
371}
372void bal_ind__bal_iface_stat_cb(grpc_c_context_t *context)
373{
374}
375void bal_ind__bal_subs_term_oper_sts_cng_cb(grpc_c_context_t *context)
376{
377}
378void bal_ind__bal_subs_term_discovery_ind_cb(grpc_c_context_t *context)
379{
380}
381void bal_ind__bal_subs_term_alarm_ind_cb(grpc_c_context_t *context)
382{
383}
384void bal_ind__bal_subs_term_dgi_ind_cb(grpc_c_context_t *context)
385{
386}
387void bal_ind__bal_subs_term_ind_cb(grpc_c_context_t *context)
388{
389}
390void bal_ind__bal_tm_queue_ind_info_cb(grpc_c_context_t *context)
391{
392}
393void bal_ind__bal_tm_sched_ind_info_cb(grpc_c_context_t *context)
394{
395}
396void bal_ind__bal_pkt_bearer_channel_rx_ind_cb(grpc_c_context_t *context)
397{
398}
399void bal_ind__bal_pkt_omci_channel_rx_ind_cb(grpc_c_context_t *context)
400{
401}
402void bal_ind__bal_pkt_ieee_oam_channel_rx_ind_cb(grpc_c_context_t *context)
403{
404}
A R Karthick1d251032017-09-06 09:38:34 -0700405
406#if 0
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530407void bal__bal_api_heartbeat_cb(grpc_c_context_t *context)
408{
409 BalHeartbeat *bal_hb;
410 BalErr bal_err;
411
412 /*
413 * Read incoming message into set_cfg
414 */
415 printf("\nRecevied HeartBeat from Adapter\n");
416 if (context->gcc_payload) {
417 context->gcc_stream->read(context, (void **)&bal_hb, 0);
418 }
419
420 printf("Received Heart Beat msg\n");
421
422 bal_err__init(&bal_err);
423
424 bal_err.err= 0;
425
426 /*
427 * Write reply back to the client
428 */
429 if (!context->gcc_stream->write(context, &bal_err, 0)) {
430 } else {
431 printf("Failed to write\n");
432 exit(1);
433 }
434
435 grpc_c_status_t status;
436 status.gcs_code = 0;
437
438 /*
439 * Finish response for RPC
440 */
441 if (context->gcc_stream->finish(context, &status)) {
442 printf("Failed to write status\n");
443 exit(1);
444 }
445 printf("\nSent HeartBeat Response to Adapter\n");
446}
A R Karthick1d251032017-09-06 09:38:34 -0700447#endif
Rajeswara Raof6b4e6c2017-08-31 17:26:27 +0530448
449/*
450 * Takes socket path as argument
451 */
452int main (int argc, char **argv)
453{
454 int i = 0;
455 grpc_c_server_t *server = NULL;
456
457 if (argc < 6) {
458 fprintf(stderr, "Missing socket path argument\n");
459 exit(1);
460 }
461
462 strcpy(coreIpPortInfo.bal_core_arg1, argv[2] /*, strlen(argv[2])*/);
463 strcpy(coreIpPortInfo.bal_core_ip_port, argv[3]/*, strlen(argv[3])*/);
464 strcpy(coreIpPortInfo.bal_core_arg2, argv[4]/*, strlen(argv[4])*/);
465 strcpy(coreIpPortInfo.bal_shared_lib_ip_port, argv[5]/*, strlen(argv[5])*/);
466
467 signal(SIGINT, sigint_handler);
468 /*
469 * Initialize grpc-c library to be used with vanilla gRPC
470 */
471 grpc_c_init(GRPC_THREADS, NULL);
472
473 /*
474 * Create server object
475 */
476 test_server = grpc_c_server_create(argv[1]);
477
478 if (test_server == NULL) {
479 printf("Failed to create server\n");
480 exit(1);
481 }
482
483 /*
484 * Initialize greeter service
485 */
486 printf("\nbal_voltha_app running.....\n");
487 bal__service_init(test_server);
488
489 /*
490 * Start server
491 */
492 grpc_c_server_start(test_server);
493
494#ifdef BAL_STUB
495 printf("\nCreating a stub thread\n");
496 create_stub_thread();
497#endif
498
499 /*
500 * Blocks server to wait to completion
501 */
502 grpc_c_server_wait(test_server);
503
504 /* code added for example Makefile to compile grpc-c along with edgecore driver */
505 bal__service_init(server);
506
507}