1 /* 2 * ipc_event.c 3 * 4 * Copyright 2001-2009 Texas Instruments, Inc. - http://www.ti.com/ 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /**************************************************************************** 20 * 21 * MODULE: IPC_Event.c 22 * 23 * PURPOSE: 24 * 25 * DESCRIPTION: 26 * ============ 27 * 28 * 29 ****************************************************************************/ 30 31 /* includes */ 32 /************/ 33 #include <sys/types.h> 34 #include <sys/socket.h> 35 #include <net/if.h> 36 #include <linux/rtnetlink.h> 37 #include <signal.h> 38 #include <sys/mman.h> 39 #include <unistd.h> 40 #include <linux/wireless.h> 41 #include "cu_osapi.h" 42 #include "oserr.h" 43 44 #include "TWDriver.h" 45 #include "STADExternalIf.h" 46 47 #include "ParsEvent.h" 48 #include "ipc_event.h" 49 50 /* defines */ 51 /***********/ 52 #define PIPE_READ 0 53 #define PIPE_WRITE 1 54 #define IPC_EVENT_KEY ((key_t)123456789) 55 56 /* IPC evemt messages to child */ 57 #define IPC_EVENT_MSG_KILL "IPC_EVENT_MSG_KILL" 58 #define IPC_EVENT_MSG_UPDATE_DEBUG_LEVEL "IPC_EVENT_MSG_UPDATE_DEBUG_LEVEL" 59 #define IPC_EVENT_MSG_MAX_LEN 50 60 61 /* local types */ 62 /***************/ 63 typedef struct IpcEvent_Shared_Memory_t 64 { 65 int pipe_fields[2]; 66 U32 event_mask; 67 union 68 { 69 S32 debug_level; 70 } content; 71 } IpcEvent_Shared_Memory_t; 72 73 /* Module control block */ 74 typedef struct IpcEvent_t 75 { 76 IpcEvent_Shared_Memory_t* p_shared_memory; 77 S32 child_process_id; 78 S32 pipe_to_child; 79 } IpcEvent_t; 80 81 typedef struct IpcEvent_Child_t 82 { 83 S32 STA_socket; 84 IpcEvent_Shared_Memory_t* p_shared_memory; 85 S32 pipe_from_parent; 86 } IpcEvent_Child_t; 87 88 /* local variables */ 89 /*******************/ 90 VOID g_tester_send_event(U8 event_index); 91 /* local fucntions */ 92 /*******************/ 93 static VOID IpcEvent_SendMessageToChild(IpcEvent_t* pIpcEvent, PS8 msg) 94 { 95 write(pIpcEvent->pipe_to_child, msg, os_strlen(msg)); 96 } 97 98 static S32 IpcEvent_Sockets_Open(VOID) 99 { 100 S32 skfd; 101 struct sockaddr_nl local; 102 103 skfd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); 104 if (skfd < 0) 105 { 106 return -1; 107 } 108 109 os_memset(&local, 0, sizeof(local)); 110 local.nl_family = AF_NETLINK; 111 local.nl_groups = RTMGRP_LINK; 112 if (bind(skfd, (struct sockaddr *) &local, sizeof(local)) < 0) 113 { 114 close(skfd); 115 return -2; 116 } 117 118 return skfd; 119 } 120 121 static inline VOID IpcEvent_Sockets_Close(S32 skfd) 122 { 123 close(skfd); 124 } 125 126 void ProcessLoggerMessage(PU8 data, U16 len); 127 128 static VOID IpcEvent_PrintEvent(IpcEvent_Child_t* pIpcEventChild, U32 EventId, TI_UINT8* pData, S32 DataLen) 129 { 130 131 if(pIpcEventChild->p_shared_memory->event_mask & (1<<EventId)) 132 { 133 switch(EventId) 134 { 135 case IPC_EVENT_DISASSOCIATED: 136 { 137 OS_802_11_DISASSOCIATE_REASON_T *pDisAssoc; 138 139 if (NULL == pData) 140 { 141 return; 142 } 143 else 144 { 145 pDisAssoc = (OS_802_11_DISASSOCIATE_REASON_T*)pData; 146 } 147 148 switch(pDisAssoc->eDisAssocType) 149 { 150 case OS_DISASSOC_STATUS_UNSPECIFIED: 151 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated with unspecified reason (User/SG/Recovery)\n"); 152 break; 153 case OS_DISASSOC_STATUS_AUTH_REJECT: 154 if (pDisAssoc->uStatusCode == STATUS_PACKET_REJ_TIMEOUT) 155 { 156 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to no Auth response \n"); 157 } 158 else 159 { 160 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to Auth response packet with reason = %d\n", pDisAssoc->uStatusCode); 161 } 162 break; 163 case OS_DISASSOC_STATUS_ASSOC_REJECT: 164 if (pDisAssoc->uStatusCode == STATUS_PACKET_REJ_TIMEOUT) 165 { 166 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to no Assoc response \n"); 167 } 168 else 169 { 170 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to Assoc response packet with reason = %d\n", pDisAssoc->uStatusCode); 171 } 172 break; 173 case OS_DISASSOC_STATUS_SECURITY_FAILURE: 174 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to RSN failure\n"); 175 break; 176 case OS_DISASSOC_STATUS_AP_DEAUTHENTICATE: 177 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to AP deAuthenticate packet with reason = %d\n", pDisAssoc->uStatusCode); 178 break; 179 case OS_DISASSOC_STATUS_AP_DISASSOCIATE: 180 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to AP disAssoc packet with reason = %d\n", pDisAssoc->uStatusCode); 181 break; 182 case OS_DISASSOC_STATUS_ROAMING_TRIGGER: 183 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated due to roaming trigger = %d\n", pDisAssoc->uStatusCode); 184 break; 185 default: 186 os_error_printf(CU_MSG_ERROR, "CLI Event - Disassociated with unknown reason = %d\n", pDisAssoc->eDisAssocType); 187 break; 188 } 189 190 break; /* the end of the IPC_EVENT_DISASSOCIATED case */ 191 } 192 case IPC_EVENT_ASSOCIATED: 193 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_ASSOCIATED\n"); 194 break; 195 case IPC_EVENT_MEDIA_SPECIFIC: 196 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_MEDIA_SPECIFIC\n"); 197 break; 198 case IPC_EVENT_SCAN_COMPLETE: 199 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_SCAN_COMPLETE\n"); 200 break; 201 /* custom events */ 202 case IPC_EVENT_SCAN_STOPPED: 203 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_SCAN_STOPPED\n"); 204 break; 205 case IPC_EVENT_LINK_SPEED: 206 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_LINK_SPEED\n"); 207 break; 208 case IPC_EVENT_AUTH_SUCC: 209 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_AUTH_SUCC\n"); 210 break; 211 case IPC_EVENT_CCKM_START: 212 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_CCKM_START\n"); 213 break; 214 case IPC_EVENT_EAPOL: 215 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_EAPOL\n"); 216 break; 217 case IPC_EVENT_RE_AUTH_STARTED: 218 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_RE_AUTH_STARTED\n"); 219 break; 220 case IPC_EVENT_RE_AUTH_COMPLETED: 221 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_RE_AUTH_COMPLETED\n"); 222 break; 223 case IPC_EVENT_RE_AUTH_TERMINATED: 224 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_RE_AUTH_TERMINATED\n"); 225 break; 226 case IPC_EVENT_BOUND: 227 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_BOUND\n"); 228 break; 229 case IPC_EVENT_UNBOUND: 230 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_UNBOUND\n"); 231 break; 232 case IPC_EVENT_PREAUTH_EAPOL: 233 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_PREAUTH_EAPOL\n"); 234 break; 235 case IPC_EVENT_LOW_RSSI: 236 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_LOW_RSSI\n"); 237 break; 238 case IPC_EVENT_TSPEC_STATUS: 239 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_TSPEC_STATUS\n"); 240 break; 241 case IPC_EVENT_TSPEC_RATE_STATUS: 242 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_TSPEC_RATE_STATUS\n"); 243 break; 244 case IPC_EVENT_MEDIUM_TIME_CROSS: 245 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_MEDIUM_TIME_CROSS\n"); 246 break; 247 case IPC_EVENT_ROAMING_COMPLETE: 248 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_ROAMING_COMPLETE\n"); 249 break; 250 case IPC_EVENT_EAP_AUTH_FAILURE: 251 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_EAP_AUTH_FAILURE\n"); 252 break; 253 case IPC_EVENT_WPA2_PREAUTHENTICATION: 254 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_WPA2_PREAUTHENTICATION\n"); 255 break; 256 case IPC_EVENT_TRAFFIC_INTENSITY_THRESHOLD_CROSSED: 257 { 258 U32 *crossInfo = (U32 *)pData; 259 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_TRAFFIC_INTENSITY_THRESHOLD_CROSSED\n"); 260 os_error_printf(CU_MSG_ERROR, (PS8)"Threshold(High=0, Low=1) crossed= %d\n", crossInfo[0]); 261 os_error_printf(CU_MSG_ERROR, (PS8)"Direction(Above=0, Below=1) crossed= %d\n", crossInfo[1]); 262 break; 263 } 264 case IPC_EVENT_SCAN_FAILED: 265 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_SCAN_FAILED\n"); 266 break; 267 case IPC_EVENT_WPS_SESSION_OVERLAP: 268 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_WPS_SESSION_OVERLAP\n"); 269 break; 270 case IPC_EVENT_RSSI_SNR_TRIGGER_0: 271 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_RSSI_SNR_TRIGGER_0, Data = %d\n", (S8)(*pData)); 272 break; 273 case IPC_EVENT_RSSI_SNR_TRIGGER_1: 274 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_RSSI_SNR_TRIGGER_1, Data = %d\n", (S8)(*pData)); 275 break; 276 case IPC_EVENT_RSSI_SNR_TRIGGER: 277 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_RSSI_SNR_TRIGGER, Data = %d\n", (S8)(*pData)); 278 break; 279 case IPC_EVENT_TIMEOUT: 280 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_TIMEOUT\n"); 281 break; 282 case IPC_EVENT_GWSI: 283 os_error_printf(CU_MSG_ERROR, (PS8)"IpcEvent_PrintEvent - received IPC_EVENT_GWSI\n"); 284 break; 285 case IPC_EVENT_LOGGER: 286 #ifdef ETH_SUPPORT 287 ProcessLoggerMessage(pData, (U16)DataLen); 288 #endif 289 break; 290 default : 291 os_error_printf(CU_MSG_ERROR, (PS8)"**** Unknow EventId %d ****\n", EventId); 292 } 293 } 294 } 295 296 static VOID IpcEvent_wext_event_wireless(IpcEvent_Child_t* pIpcEventChild, PS8 data, S32 len) 297 { 298 struct iw_event iwe; 299 struct stream_descr stream; 300 S32 ret; 301 IPC_EV_DATA* pEvent; 302 U32 EventId = 0; 303 304 /* init the event stream */ 305 os_memset((char *)&stream, '\0', sizeof(struct stream_descr)); 306 stream.current = (char *)data; 307 stream.end = (char *)(data + len); 308 309 do 310 { 311 /* Extract an event and print it */ 312 ret = ParsEvent_GetEvent(&stream, &iwe); 313 314 if(ret <= 0) 315 break; 316 317 switch (iwe.cmd) 318 { 319 case SIOCGIWAP: 320 if((iwe.u.ap_addr.sa_data[0] == 0) && 321 (iwe.u.ap_addr.sa_data[1] == 0) && 322 (iwe.u.ap_addr.sa_data[2] == 0) && 323 (iwe.u.ap_addr.sa_data[3] == 0) && 324 (iwe.u.ap_addr.sa_data[4] == 0) && 325 (iwe.u.ap_addr.sa_data[5] == 0)) 326 { 327 EventId=IPC_EVENT_DISASSOCIATED; 328 IpcEvent_PrintEvent(pIpcEventChild, EventId, NULL,0); 329 } 330 else 331 { 332 EventId=IPC_EVENT_ASSOCIATED; 333 IpcEvent_PrintEvent(pIpcEventChild, EventId, NULL,0); 334 } 335 break; 336 case IWEVMICHAELMICFAILURE: 337 EventId=IPC_EVENT_MEDIA_SPECIFIC; 338 IpcEvent_PrintEvent(pIpcEventChild, EventId, NULL,0); 339 break; 340 case IWEVCUSTOM: 341 pEvent = (IPC_EV_DATA*)iwe.u.data.pointer; 342 if (pEvent) 343 { 344 EventId = (U32)pEvent->EvParams.uEventType; 345 IpcEvent_PrintEvent (pIpcEventChild, EventId, pEvent->uBuffer, pEvent->uBufferSize); 346 } 347 break; 348 case SIOCGIWSCAN: 349 EventId=IPC_EVENT_SCAN_COMPLETE; 350 IpcEvent_PrintEvent(pIpcEventChild, EventId, NULL,0); 351 break; 352 case IWEVASSOCREQIE: 353 /* NOP */ 354 break; 355 case IWEVASSOCRESPIE: 356 /* NOP */ 357 break; 358 case IWEVPMKIDCAND: 359 EventId=IPC_EVENT_MEDIA_SPECIFIC; 360 IpcEvent_PrintEvent(pIpcEventChild, EventId, NULL,0); 361 break; 362 } 363 364 g_tester_send_event((U8) EventId); 365 366 } 367 while(1); 368 } 369 370 static VOID IpcEvent_wext_event_rtm_newlink(IpcEvent_Child_t* pIpcEventChild, struct nlmsghdr *h, 371 S32 len) 372 { 373 struct ifinfomsg *ifi; 374 S32 attrlen, nlmsg_len, rta_len; 375 struct rtattr * attr; 376 377 if (len < sizeof(*ifi)) 378 return; 379 380 ifi = NLMSG_DATA(h); 381 382 /* 383 if ((if_nametoindex("wlan") != ifi->ifi_index) && (if_nametoindex("wifi") != ifi->ifi_index)) 384 { 385 os_error_printf(CU_MSG_ERROR, "ERROR - IpcEvent_wext_event_rtm_newlink - Ignore event for foreign ifindex %d", 386 ifi->ifi_index); 387 return; 388 } 389 */ 390 391 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg)); 392 393 attrlen = h->nlmsg_len - nlmsg_len; 394 if (attrlen < 0) 395 return; 396 397 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len); 398 399 rta_len = RTA_ALIGN(sizeof(struct rtattr)); 400 while (RTA_OK(attr, attrlen)) { 401 if (attr->rta_type == IFLA_WIRELESS) 402 { 403 IpcEvent_wext_event_wireless(pIpcEventChild, ((PS8) attr) + rta_len, 404 attr->rta_len - rta_len); 405 } 406 else if (attr->rta_type == IFLA_IFNAME) 407 { 408 os_error_printf(CU_MSG_WARNING, (PS8)"WARNING - IpcEvent_wext_event_rtm_newlink - unsupported rta_type = IFLA_IFNAME\n"); 409 410 } 411 attr = RTA_NEXT(attr, attrlen); 412 } 413 } 414 415 static VOID IpcEvent_Handle_STA_Event(IpcEvent_Child_t* pIpcEventChild) 416 { 417 S8 buf[512]; 418 S32 left; 419 struct sockaddr_nl from; 420 socklen_t fromlen; 421 struct nlmsghdr *h; 422 423 fromlen = sizeof(from); 424 left = recvfrom(pIpcEventChild->STA_socket, buf, sizeof(buf), MSG_DONTWAIT, 425 (struct sockaddr *) &from, &fromlen); 426 if (left < 0) 427 { 428 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Handle_STA_Event - cant recv from socket %X .\n", 429 pIpcEventChild->STA_socket); 430 return; 431 } 432 433 h = (struct nlmsghdr *) buf; 434 435 while (left >= sizeof(*h)) 436 { 437 S32 len, plen; 438 439 len = h->nlmsg_len; 440 plen = len - sizeof(*h); 441 if (len > left || plen < 0) { 442 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Handle_STA_Event - Malformed netlink message: len=%d left=%d plen=%d", 443 len, left, plen); 444 break; 445 } 446 447 switch (h->nlmsg_type) 448 { 449 case RTM_NEWLINK: 450 IpcEvent_wext_event_rtm_newlink(pIpcEventChild, h, plen); 451 break; 452 } 453 454 len = NLMSG_ALIGN(len); 455 left -= len; 456 h = (struct nlmsghdr *) ((char *) h + len); 457 } 458 459 if (left > 0) 460 { 461 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Handle_STA_Event - %d extra bytes in the end of netlink ", 462 left); 463 IpcEvent_Handle_STA_Event(pIpcEventChild); 464 } 465 466 } 467 468 static S32 IpcEvent_Handle_Parent_Event(IpcEvent_Child_t* pIpcEventChild) 469 { 470 S8 msg[IPC_EVENT_MSG_MAX_LEN]; 471 472 S32 msgLen = read(pIpcEventChild->pipe_from_parent,msg, IPC_EVENT_MSG_MAX_LEN); 473 msg[msgLen] = 0; 474 475 if(!os_strcmp(msg, (PS8)IPC_EVENT_MSG_KILL)) 476 { 477 return TRUE; 478 } 479 if(!os_strcmp(msg, (PS8)IPC_EVENT_MSG_UPDATE_DEBUG_LEVEL)) 480 { 481 /* g_debug_level= pIpcEventChild->p_shared_memory->content.debug_level;*/ 482 return FALSE; 483 }else 484 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Handle_Parent_Event - unknown msgLen=%d msg=|%s| \n",msgLen,msg); 485 486 return FALSE; 487 488 } 489 490 static VOID IpcEvent_Child_Destroy(IpcEvent_Child_t* pIpcEventChild) 491 { 492 if(pIpcEventChild->STA_socket) 493 { 494 IpcEvent_Sockets_Close(pIpcEventChild->STA_socket); 495 } 496 497 } 498 499 static VOID IpcEvent_Child(IpcEvent_Child_t* pIpcEventChild) 500 { 501 /* open the socket from the driver */ 502 pIpcEventChild->STA_socket = IpcEvent_Sockets_Open(); 503 if(pIpcEventChild->STA_socket < 0) 504 { 505 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Create - cant open socket for communication with the driver (%d)\n",pIpcEventChild->STA_socket); 506 return; 507 } 508 509 while(1) 510 { 511 fd_set read_set; /* File descriptors for select */ 512 S32 ret; 513 514 FD_ZERO(&read_set); 515 FD_SET(pIpcEventChild->STA_socket, &read_set); 516 FD_SET(pIpcEventChild->pipe_from_parent, &read_set); 517 518 #ifndef ANDROID 519 ret = select(max(pIpcEventChild->pipe_from_parent,pIpcEventChild->STA_socket) + 1, 520 &read_set, NULL, NULL, NULL); 521 #else 522 ret = select(pIpcEventChild->STA_socket + 1, 523 &read_set, NULL, NULL, NULL); 524 #endif 525 526 if(ret < 0) 527 { 528 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Child - Unhandled signal - exiting...\n"); 529 break; 530 } 531 if(ret == 0) { continue; } /* Check for interface discovery events. */ 532 if(FD_ISSET(pIpcEventChild->STA_socket, &read_set)) 533 IpcEvent_Handle_STA_Event(pIpcEventChild); 534 535 #ifndef ANDROID 536 if(FD_ISSET(pIpcEventChild->pipe_from_parent, &read_set)) 537 { 538 S32 exit = IpcEvent_Handle_Parent_Event(pIpcEventChild); 539 if(exit) 540 break; 541 } 542 #endif 543 544 } 545 546 IpcEvent_Child_Destroy(pIpcEventChild); 547 } 548 549 /* functions */ 550 /*************/ 551 THandle IpcEvent_Create(VOID) 552 { 553 IpcEvent_t* pIpcEvent = (IpcEvent_t*)os_MemoryCAlloc(sizeof(IpcEvent_t), sizeof(U8)); 554 if(pIpcEvent == NULL) 555 { 556 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Create - cant allocate control block\n"); 557 return NULL; 558 } 559 560 /* create a shared memory space */ 561 pIpcEvent->p_shared_memory = mmap(0, sizeof(IpcEvent_Shared_Memory_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); 562 if ( pIpcEvent->p_shared_memory == ((PVOID)-1)) 563 { 564 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Create - cant allocate shared memory\n"); 565 IpcEvent_Destroy(pIpcEvent); 566 return NULL; 567 } 568 569 /* create a pipe */ 570 pipe(pIpcEvent->p_shared_memory->pipe_fields); 571 572 /* set the event mask to all disabled */ 573 pIpcEvent->p_shared_memory->event_mask = 0; 574 575 /* Create a child process */ 576 pIpcEvent->child_process_id = fork(); 577 578 if (0 == pIpcEvent->child_process_id) 579 { 580 /******************/ 581 /* Child process */ 582 /****************/ 583 IpcEvent_Child_t* pIpcEventChild = (IpcEvent_Child_t*)os_MemoryCAlloc(sizeof(IpcEvent_Child_t), sizeof(U8)); 584 if(pIpcEventChild == NULL) 585 { 586 os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcEvent_Create - cant allocate child control block\n"); 587 _exit(1); 588 } 589 590 pIpcEventChild->p_shared_memory = pIpcEvent->p_shared_memory; 591 592 pIpcEventChild->pipe_from_parent = pIpcEventChild->p_shared_memory->pipe_fields[PIPE_READ]; 593 close(pIpcEventChild->p_shared_memory->pipe_fields[PIPE_WRITE]); 594 595 IpcEvent_Child(pIpcEventChild); 596 597 os_MemoryFree(pIpcEventChild); 598 599 _exit(0); 600 } 601 602 pIpcEvent->pipe_to_child = pIpcEvent->p_shared_memory->pipe_fields[PIPE_WRITE]; 603 close(pIpcEvent->p_shared_memory->pipe_fields[PIPE_READ]); 604 605 return pIpcEvent; 606 } 607 608 VOID IpcEvent_Destroy(THandle hIpcEvent) 609 { 610 IpcEvent_t* pIpcEvent = (IpcEvent_t*)hIpcEvent; 611 612 if((pIpcEvent->p_shared_memory != ((PVOID)-1)) && (pIpcEvent->p_shared_memory)) 613 { 614 munmap(pIpcEvent->p_shared_memory, sizeof(IpcEvent_Shared_Memory_t)); 615 } 616 617 /* kill child process */ 618 kill(pIpcEvent->child_process_id, SIGKILL); 619 620 os_MemoryFree(pIpcEvent); 621 622 } 623 624 S32 IpcEvent_EnableEvent(THandle hIpcEvent, U32 event) 625 { 626 IpcEvent_t* pIpcEvent = (IpcEvent_t*)hIpcEvent; 627 628 if(pIpcEvent->p_shared_memory->event_mask & (1 << event)) 629 { 630 return EOALERR_IPC_EVENT_ERROR_EVENT_ALREADY_ENABLED; 631 } 632 else 633 { 634 pIpcEvent->p_shared_memory->event_mask |= (1 << event); 635 } 636 637 return OK; 638 639 } 640 641 S32 IpcEvent_DisableEvent(THandle hIpcEvent, U32 event) 642 { 643 IpcEvent_t* pIpcEvent = (IpcEvent_t*)hIpcEvent; 644 645 if(!(pIpcEvent->p_shared_memory->event_mask & (1 << event))) 646 { 647 return EOALERR_IPC_EVENT_ERROR_EVENT_ALREADY_DISABLED; 648 } 649 else 650 { 651 pIpcEvent->p_shared_memory->event_mask &= ~(1 << event); 652 } 653 654 return OK; 655 } 656 657 S32 IpcEvent_UpdateDebugLevel(THandle hIpcEvent, S32 debug_level) 658 { 659 IpcEvent_t* pIpcEvent = (IpcEvent_t*)hIpcEvent; 660 661 pIpcEvent->p_shared_memory->content.debug_level = debug_level; 662 IpcEvent_SendMessageToChild(pIpcEvent, (PS8)IPC_EVENT_MSG_UPDATE_DEBUG_LEVEL); 663 664 return OK; 665 } 666 667