Example #1
0
void at_sockwrite_wnc(const char * s)
{
  string * pRespStr;
  char num2str[6];
  size_t sLen = strlen(s);
  if (sLen <= 99999)
  {
    string cmd_str("AT@SOCKWRITE=1,");
    itoa(sLen, num2str, 10);
    cmd_str += num2str;
    cmd_str += ",\"";
    while(*s != '\0')
    {
      itoa((int)*s++, num2str, 16);
      // Always 2-digit ascii hex:
      if (strlen(num2str) == 1)
      {
        num2str[2] = '\0';
        num2str[1] = num2str[0];
        num2str[0] = '0';
      }
      cmd_str += num2str;
    }
    cmd_str += "\"";
    send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS);
  }
  else
    pc.puts("sockwrite Err, string to long\r\n");
}
Example #2
0
void at_sockopen_wnc(const string & ipStr, const char * port )
{
  string * pRespStr;
  send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS);
  string cmd_str("AT@SOCKCONN=1,\"");
  cmd_str += ipStr;
  cmd_str += "\",";
  cmd_str += port;
  send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS);
}
Example #3
0
static void os_read(char *buf, int len)
{
	cmd_str("uname -o", buf, len);

	int i;
	for (i = strlen(buf) - 1; i >= 0; i--) {
		if (!isspace(buf[i]))
			break;
		buf[i] = 0;
	}
}
Example #4
0
void at_init_wnc(void)
{
  string * pRespStr;
  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
  send_wnc_cmd("ATE1", &pRespStr, WNC_TIMEOUT_MS);           // Echo ON
  string cmd_str("AT%PDNSET=1,");
  cmd_str += MY_APN_STR;
  cmd_str += ",IP";
  send_wnc_cmd(cmd_str.data(), &pRespStr, 2*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
  send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);  // Internet services enabled
  send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
}
Example #5
0
File: utils.c Project: lclc/mcu
void utils_decrypt_report(const char *report)
{
    int decrypt_len;
    char *dec;

    memset(decrypted_report, 0, sizeof(decrypted_report));

    yajl_val json_node = yajl_tree_parse(report, NULL, 0);

    if (!json_node) {
        strcpy(decrypted_report, "/* error: Failed to parse report. */");
        return;
    }

    size_t i, r = json_node->u.object.len;
    for (i = 0; i < r; i++) {
        const char *ciphertext_path[] = { cmd_str(CMD_ciphertext), (const char *) 0 };
        const char *echo_path[] = { "echo", (const char *) 0 };
        const char *ciphertext = YAJL_GET_STRING(yajl_tree_get(json_node, ciphertext_path,
                                 yajl_t_string));
        const char *echo = YAJL_GET_STRING(yajl_tree_get(json_node, echo_path, yajl_t_string));
        if (ciphertext) {
            dec = aes_cbc_b64_decrypt((const unsigned char *)ciphertext, strlens(ciphertext),
                                      &decrypt_len, PASSWORD_STAND);
            if (!dec) {
                strcpy(decrypted_report, "/* error: Failed to decrypt. */");
                goto exit;
            }

            sprintf(decrypted_report, "/* ciphertext */ %.*s", decrypt_len, dec);
            free(dec);
            goto exit;
        } else if (echo) {
            dec = aes_cbc_b64_decrypt((const unsigned char *)echo, strlens(echo), &decrypt_len,
                                      PASSWORD_VERIFY);
            if (!dec) {
                strcpy(decrypted_report, "/* error: Failed to decrypt echo. */");
                goto exit;
            }

            sprintf(decrypted_report, "/* echo */ %.*s", decrypt_len, dec);
            free(dec);
            goto exit;
        }
    }
    strcpy(decrypted_report, report);
exit:
    yajl_tree_free(json_node);
    return;
}
Example #6
0
unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0)
{
  unsigned i;
  string * pRespStr;
  string cmd_str("AT@SOCKREAD=1,");
  if (n <= 1500)
  {
    char num2str[6];
    
    itoa(n, num2str, 10);
    cmd_str += num2str;
    retries += 1;
    while (retries--)
    {
      // Assuming someone is sending then calling this to receive response, invoke
      // a pause to give the response some time to come back and then also
      // between each retry.
      wait_ms(10);
      
      send_wnc_cmd(cmd_str.data(), &pRespStr, WNC_TIMEOUT_MS);
      size_t pos_start = pRespStr->find("\"")  + 1;
      size_t pos_end   = pRespStr->rfind("\"") - 1;
      i = pos_end - pos_start + 1;
      if (i > 0)
      {
        retries = 0;  // If any data found stop retrying
        string byte;
        pS->erase();
        while (pos_start < pos_end)
        {
          byte = pRespStr->substr(pos_start, 2);
          *pS += (char)strtol(byte.data(), NULL, 16);
          pos_start += 2;
        }
        return i;
      }
    }
  }
  else
    pc.puts("sockread Err, to many to read\r\n");
  
  return 0;
}
Example #7
0
static void dmidecode_read(const char *field_name, char *buf, int len)
{
	char cmd[128];

	memset(buf, 0, len);

	snprintf(cmd, sizeof(cmd), "dmidecode -s %s", field_name);
	if (!cmd_str(cmd, buf, len))
		return;

	int actual_len = strlen(buf);
	switch (buf[actual_len-1]) {
		case '\r':
		case '\n':
			buf[actual_len-1] = 0;
			actual_len--;
			break;
	}

	sha1_calc((unsigned char *)buf, actual_len, buf, len);
}
Example #8
0
int WINAPI 
WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLn, int nShowCmd)
{     
    bool good_engine;
    std::string cmd_str(lpCmdLn);
    std::vector<std::string> args;    

    /* get 'our' module name */
    SmartBuffer<CHAR> module_buf(MAX_PATH);
    GetModuleFileName(NULL, module_buf.get(), MAX_PATH);

    /* start  logging */
    std::string logpath(TOSDB_LOG_PATH);
    logpath.append(LOG_NAME);
    StartLogging(logpath.c_str()); 

    /* add the appropriate engine name to the module path */
    std::string path(module_buf.get()); 
    std::string serv_ext(path.begin() + path.find_last_of("-"), path.end()); 

#ifdef _DEBUG
    if(serv_ext != "-x86_d.exe" && serv_ext != "-x64_d.exe"){
#else
    if(serv_ext != "-x86.exe" && serv_ext != "-x64.exe"){
#endif	
        TOSDB_LogH("STARTUP", "service path doesn't provide proper extension");
        return 1;
    }

    path.erase(path.find_last_of("\\")); 
    engine_path = path;
    engine_path.append("\\").append(ENGINE_BASE_NAME).append(serv_ext); 

    GetSystemInfo(&sys_info);    

    ParseArgs(args,cmd_str);
    
    size_t argc = args.size();
    int admin_pos = 0;
    int no_service_pos = 0;	

    /* look for --admin and/or --noservice args */
    if(argc > 0 && args[0] == "--admin")            
        admin_pos = 1;
    else if(argc > 1 && args[1] == "--admin") 
        admin_pos = 2;
    
    if(argc > 0 && args[0] == "--noservice")            
        no_service_pos = 1;
    else if(argc > 1 && args[1] == "--noservice") 
        no_service_pos = 2;        

    switch(argc){ /* look for custom_session arg */
    case 0:
        break;
    case 1:
        if(admin_pos == 0 && no_service_pos == 0)
            custom_session = std::stoi(args[0]);
        break;
    case 2:
        if( (admin_pos == 1 && no_service_pos != 2) 
            || (no_service_pos == 1 && admin_pos != 2))
        {
            custom_session = std::stoi(args[1]);
        }
        break;
    case 3:
        if(admin_pos > 0 && no_service_pos > 0)
            custom_session = std::stoi(args[2]);
        break;
    default:
        std::string serr("invalid # of args: ");
        serr.append(std::to_string(argc));
        TOSDB_LogH("STARTUP",serr.c_str());
        return 1;
    }     
   
    std::stringstream ss_args;
    ss_args << "argc: " << std::to_string(argc) 
            << " custom_session: " << std::to_string(custom_session) 
            << " admin_pos: " << std::to_string(admin_pos) 
            << " no_service_pos: " << std::to_string(no_service_pos);
		
    TOSDB_Log("STARTUP", std::string("lpCmdLn: ").append(cmd_str).c_str() );
    TOSDB_Log("STARTUP", ss_args.str().c_str() );

    integrity_level = admin_pos > 0 ? "High Mandatory Level" : "Medium Mandatory Level"; 
    
    /* populate the engine command and if --noservice is passed jump right into
       the engine via SpawnRestrictedProcess; otherwise Start the service which
       will handle that for us 
       
       prepend '--spawned' so engine knows *we* called it; if someone
       else passes '--spawned' they deserve what they get */

    if(no_service_pos > 0){       
        is_service = false;

        TOSDB_Log("STARTUP", "starting tos-databridge-engine.exe directly(NOT A SERVICE)");

        good_engine = SpawnRestrictedProcess("--spawned --noservice", custom_session); 
        if(!good_engine){      
            std::string serr("failed to spawn ");
            serr.append(engine_path).append(" --spawned --noservice");
            TOSDB_LogH("STARTUP", serr.c_str());         
        }
    }else{    
        SERVICE_TABLE_ENTRY dTable[] = {
            {SERVICE_NAME,ServiceMain},
            {NULL,NULL}
        };        

        /* START SERVICE */	
        if( !StartServiceCtrlDispatcher(dTable) ){
            TOSDB_LogH("STARTUP", "StartServiceCtrlDispatcher() failed. "  
                                  "(Be sure to use an appropriate Window's tool to start the service "
                                  "(e.g SC.exe, Services.msc) or pass '--noservice' to run directly.)");
        }
    }

    StopLogging();
    return 0;
}