示例#1
0
文件: pers_cli.c 项目: sigmond/mpl
static int checkCommand(mpl_list_t *cmdMsg)
{
    char *buf = NULL;
    int len;
    mpl_param_element_t *elem_p;
    mpl_list_t *check_result_list_p = NULL;
    mpl_pack_options_t options = MPL_PACK_OPTIONS_DEFAULT;
    const char *prompt = "Protocol error: ";
    options.force_field_pack_mode = true;

    elem_p = mpl_param_list_find(PERS_PARAM_ID(Req), cmdMsg);
    if (elem_p == NULL) {
        printf("%sno command\n", prompt);
        return -1;
    }
    if (personnel_checkBag_Req(elem_p, &check_result_list_p)) {
        len = mpl_param_list_pack_extended(check_result_list_p,
                                           NULL,
                                           0,
                                           &options);
        buf = calloc(1, len + 1);
        strcat(buf, prompt);
        if (buf != NULL) {
            (void)mpl_param_list_pack_extended(check_result_list_p,
                                               buf,
                                               len+1,
                                               &options);
            printf("%s%s\n", prompt, buf);
            free(buf);
        }
        else {
            printf("%smemory error\n", prompt);
        }
        mpl_param_list_destroy(&check_result_list_p);
        return -1;
    }
    return 0;
}
示例#2
0
int main() {
  int client_tag = 1;
  int handle;
  char lan_device_p[] = "usb0";
  char wan_device_p[] = "rmnet0";
  char dns_server_p[] = "10.10.10.10";

  printf("\n");
  printf("#############\n");
  printf("Starting test\n");
  printf("#############\n");

  if (initStecom() < 0)
  {
    printf("Failed to start stecom\n");
    goto error;
  }
  TESTNOTNEG(sterc_init(NULL, &my_log_func));

  printf("Test 1 - create\n");
  {
    mpl_msg_t           *request_p  = NULL;
    mpl_msg_t resp;

    request_p = mpl_req_msg_alloc(STERC_PARAM_SET_ID);
    if (NULL == request_p) {
        printf("mpl_req_msg_alloc failed!\n");
        goto error;
    }

    request_p->req.id = sterc_create;
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_lan_device, lan_device_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_wan_device, wan_device_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_wan_dns, dns_server_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_ct, &client_tag));
    TESTNOTNEG(sendToStecomCtrl(request_p));
    mpl_msg_free((mpl_msg_t *)request_p);

    TESTNOTNEG(waitAndReadStecomResp(&resp));
    TESTBOOL(resp.common.type, sterc_msgtype_resp);
    TESTBOOL(resp.common.id, sterc_create);
    TESTBOOL(MPL_GET_PARAM_VALUE_FROM_LIST(int,sterc_paramid_ct,(resp.common.param_list_p)), client_tag);
    handle = MPL_GET_PARAM_VALUE_FROM_LIST(int,sterc_paramid_handle,(resp.common.param_list_p));
    TESTBOOL(resp.resp.result, sterc_result_ok);
    mpl_param_list_destroy(&resp.common.param_list_p);

  }

  printf("Test 2 - set parameter req\n");
  {
    mpl_msg_t           *request_p  = NULL;
    mpl_msg_t resp;
    memcpy(lan_device_p, "usb1\0", 5);

    request_p = mpl_req_msg_alloc(STERC_PARAM_SET_ID);
    if (NULL == request_p) {
        printf("mpl_req_msg_alloc failed!\n");
        goto error;
    }

    request_p->req.id = sterc_set;
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_handle, &handle));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_lan_device, lan_device_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_ct, &client_tag));
    TESTNOTNEG(sendToStecomCtrl(request_p));
    mpl_msg_free((mpl_msg_t *)request_p);

    TESTNOTNEG(waitAndReadStecomResp(&resp));
    TESTBOOL(resp.common.type, sterc_msgtype_resp);
    TESTBOOL(resp.common.id, sterc_set);
    TESTBOOL(MPL_GET_PARAM_VALUE_FROM_LIST(int,sterc_paramid_ct,(resp.common.param_list_p)), client_tag);
    TESTBOOL(resp.resp.result, sterc_result_ok);
    mpl_param_list_destroy(&resp.common.param_list_p);
  }

  printf("Test 3 - get parameter req\n");
  {
    mpl_msg_t           *request_p  = NULL;
    mpl_msg_t resp;
    char* lan_device;
    mpl_param_element_t       *param_element_p  = NULL;

    request_p = mpl_req_msg_alloc(STERC_PARAM_SET_ID);
    if (NULL == request_p) {
        printf("mpl_req_msg_alloc failed!\n");
        goto error;
    }

    request_p->req.id = sterc_get;
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_handle, &handle));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_lan_device, lan_device_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_ct, &client_tag));
    TESTNOTNEG(sendToStecomCtrl(request_p));
    mpl_msg_free((mpl_msg_t *)request_p);

    TESTNOTNEG(waitAndReadStecomResp(&resp));
    TESTBOOL(resp.common.type, sterc_msgtype_resp);
    TESTBOOL(resp.common.id, sterc_get);
    TESTBOOL(MPL_GET_PARAM_VALUE_FROM_LIST(int,sterc_paramid_ct,(resp.common.param_list_p)), client_tag);

    param_element_p = mpl_param_list_find(sterc_paramid_lan_device, resp.common.param_list_p);
    if (param_element_p == NULL) {
      printf("Could not find lan device parameter in response\n");
      goto error;
    }
    lan_device = (char*) param_element_p->value_p;
    TESTBOOL(strncmp(lan_device, lan_device_p, 5),0);
    TESTBOOL(resp.resp.result, sterc_result_ok);
    mpl_param_list_destroy(&resp.common.param_list_p);
  }

  printf("Test 4 - destroy req\n");
  {
    mpl_msg_t           *request_p  = NULL;
    mpl_msg_t resp;

    request_p = mpl_req_msg_alloc(STERC_PARAM_SET_ID);
    if (NULL == request_p) {
        printf("mpl_req_msg_alloc failed!\n");
        goto error;
    }

    request_p->req.id = sterc_destroy;
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_handle, &handle));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_ct, &client_tag));
    TESTNOTNEG(sendToStecomCtrl(request_p));
    mpl_msg_free((mpl_msg_t *)request_p);

    TESTNOTNEG(waitAndReadStecomResp(&resp));
    TESTBOOL(resp.common.type, sterc_msgtype_resp);
    TESTBOOL(resp.common.id, sterc_destroy);
    TESTBOOL(MPL_GET_PARAM_VALUE_FROM_LIST(int,sterc_paramid_ct,(resp.common.param_list_p)), client_tag);
    TESTBOOL(resp.resp.result, sterc_result_ok);
    mpl_param_list_destroy(&resp.common.param_list_p);

  }

  printf("Test 5 - create without wan_device\n");
  {
    mpl_msg_t           *request_p  = NULL;
    mpl_msg_t resp;

    request_p = mpl_req_msg_alloc(STERC_PARAM_SET_ID);
    if (NULL == request_p) {
        printf("mpl_req_msg_alloc failed!\n");
        goto error;
    }

    request_p->req.id = sterc_create;
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_lan_device, lan_device_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_wan_dns, dns_server_p));
    TESTNOTNEG(mpl_add_param_to_list(&request_p->req.param_list_p, sterc_paramid_ct, &client_tag));
    TESTNOTNEG(sendToStecomCtrl(request_p));
    mpl_msg_free((mpl_msg_t *)request_p);

    TESTNOTNEG(waitAndReadStecomResp(&resp));
    TESTBOOL(resp.common.type, sterc_msgtype_resp);
    TESTBOOL(resp.common.id, sterc_create);
    TESTBOOL(resp.resp.result, sterc_result_failed_parameter_not_found);
    mpl_param_list_destroy(&resp.common.param_list_p);
  }

  closeStecom();

  printf("#######################\n");
  printf("Testing done, all pass!\n");
  printf("#######################\n\n");

  return 0;
error:

  printf("##############\n");
  printf("Testing failed\n");
  printf("##############\n\n");
  return -1;
}
static bool psccclient_selector_callback_event(int fd, void *data_p)
{
    psccclient_t            *psccclient_p  = (psccclient_t *)data_p;
    mpl_msg_event_t         *event_p       = NULL;
    exe_request_record_t    *record_p      = NULL;
    int                      conn_id       = 0;
    bool                     in_iadb       = false;
    exe_request_t            request;
    bool                     event_processed = false;
    bool                     network_initiated = false;
    mpl_param_element_t      *param_elem_p = NULL;
    int res;
    bool                     sterc_event = false;

    ATC_LOG_D("fd = %d, data_p = %p", fd, data_p);

    if (NULL == psccclient_p) {
        ATC_LOG_E("psccclient_p is NULL!");
        goto exit;
    }

    if (psccclient_p->pscc_fd_event == fd) {
        event_p = mpl_req_msg_alloc(PSCC_PARAM_SET_ID);
    } else if (psccclient_p->sterc_fd_event == fd) {
        event_p = mpl_req_msg_alloc(STERC_PARAM_SET_ID);
        sterc_event = true;
    } else {
        ATC_LOG_E("invalid fd_request");
        goto exit;
    }

    if (NULL == event_p) {
        ATC_LOG_E("mpl_event_msg_alloc failed!");
        goto exit;
    }

    res = psccclient_receive_event(psccclient_p, event_p, sterc_event);

    if (res < 0) {
        ATC_LOG_E("receive_event failed!");
        goto exit;
    } else if (res == 0) {
        /* Zero is returned if peer has closed the socket */
        goto disconnect;
    }

    ATC_LOG_I("received event %d", event_p->id);

    if (PSCC_CT_PRESENT((pscc_msg_t *) event_p)) {
        /* The event included a client tag.
         * Concerns attach and detach.
         */
        int client_tag = (int)PSCC_GET_CT((pscc_msg_t *) event_p);
        record_p = request_record_get_record_from_client_tag((int)PSCC_GET_CT((pscc_msg_t *) event_p));

        ATC_LOG_I("Client Tag Present");
        if (NULL == record_p) {
            ATC_LOG_E("could not find request record");
            goto exit;
        }
    } else if (PSCC_CONNID_PRESENT((pscc_msg_t *) event_p)) {
        /* The event included a conn id.
         * Get the corresponding request from iadb.
         */

        ATC_LOG_I("CONNID Present");
        conn_id = get_connection_id(event_p);
        in_iadb = psccclient_iadb_get_at_ct_on_conn_id(conn_id, (int *)(&record_p));

        if(in_iadb && (event_p->id == pscc_event_modified) && (NULL != record_p) && (EXE_CGCMOD != record_p->request_id)) {
            ATC_LOG_I("pscc_event_modified recvd and record_p->request_id = %d",record_p->request_id);
            network_initiated = true;
        }

        /* Even if there is a pscc call waiting for a return for a particular connid, there is a chance
         * that the event might be unsolicited. Currently only occurs for (network initiated)
         * disconnected events. In that case, handle it as unsolicited. */

        if (in_iadb && event_p->id == pscc_event_disconnected) {
            param_elem_p = mpl_param_list_find(pscc_paramid_reason, event_p->param_list_p);

            if (param_elem_p != NULL && (pscc_reason_pdp_context_nw_deactivated == *(int *)(param_elem_p->value_p))) {
                ATC_LOG_I("nw initiated pscc_event_disconnected. Treat as unsolicited.");
                network_initiated = true;
            } else if (param_elem_p != NULL && (pscc_reason_pdp_context_nw_deactivated_reset == *(int *)(param_elem_p->value_p))) {
                ATC_LOG_I("nw initiated pscc_event_disconnected with reset indication. Treat as unsolicited.");
                network_initiated = true;
            }
        }

        if (NULL == record_p || !in_iadb || network_initiated) {
#ifdef EXE_USE_ATC_CUSTOMER_EXTENSIONS
            /* Note: The extended event handler needs to return false if processing
             * is not complete and further processing is required by the generic
             * handler. Return true to prevent the generic handler from ever seeing
             * the event.
             */
            event_processed = exe_extended_pscc_event_handler(psccclient_p, event_p, conn_id);
#else
            event_processed = false;
#endif
            ATC_LOG_I("Received unsolicited event not related to an outstanding request");

            /* Unsolicited event not related to an outstanding request. */
            if (!event_processed) {
                event_processed = exe_pscc_event_handler(psccclient_p, event_p, conn_id);

                if (!event_processed) {
                    ATC_LOG_E("Unknown/Unhandled event! (%d)", event_p->id);
                }
            }

            goto exit;
        }
    } else if (STERC_HANDLE_PRESENT((pscc_msg_t *) event_p)) {
        int sterc_handle = (int)STERC_GET_HANDLE((pscc_msg_t *) event_p);

        in_iadb = psccclient_iadb_get_at_ct_on_sterc_handle(sterc_handle, (int *)(&record_p));

        ATC_LOG_I("Sterc Handle Present");
        if (in_iadb && event_p->id == sterc_event_disconnected) {
            param_elem_p = mpl_param_list_find(sterc_paramid_reason, event_p->param_list_p);

            if (param_elem_p != NULL) {
                ATC_LOG_I("Received sterc disconnect reason: (%d)", *(int *)(param_elem_p->value_p));
            }
        }

        if (NULL == record_p || !in_iadb) {
            ATC_LOG_I("Received unsolicited event not related to an outstanding request with Sterc Handle Present");
            /* Unsolicited event not related to an outstanding request. */
            event_processed = exe_pscc_event_handler(psccclient_p, event_p, sterc_handle);

            if (!event_processed) {
                ATC_LOG_E("Received unknown sterc event! (%d)", event_p->id);
            }

            goto exit;
        }
    } else {
        /* No client tag and no connection id. */
        ATC_LOG_E("No CT and no CONNID");
        goto exit;
    }

    ATC_LOG_I("Setting event to record");
    if (!psccclient_set_pscc_event_p(record_p, event_p)) {
        ATC_LOG_E("failed to set event_p to record!");
        goto exit;
    }

    request = request_record_get_request(record_p);

    if (EXE_SUCCESS != request(record_p)) {
        ATC_LOG_E("request returned unsuccessfully, failed!");
    }

exit:
    mpl_msg_free((mpl_msg_t *)event_p);
    return true;   /* Returning false will remove the fd from the selector. */

disconnect:
    ATC_LOG_E("********************************************************");
    ATC_LOG_E("***** at_core lost contact with pscc daemon        *****");
    ATC_LOG_E("********************************************************");
    mpl_msg_free((mpl_msg_t *)event_p);

    /* Do nothing. Let the receive socket callback clean-up. */

    return false;   /* Returning false will remove the fd from the selector. */
}