Пример #1
0
// return 1 if the value is the bool value true, number 1, or even the string "true", false otherwise
int j0g_test(char *key, char *json, unsigned short *index)
{
    char *val = j0g_str(key, json, index);
    if(!val) return 0;
    if(strcmp(val, "true") == 0) return 1;
    if(strcmp(val, "1") == 0) return 1;
    return 0;
}
Пример #2
0
path_t path_parse(char *json, int len)
{
  unsigned short js[64];
  path_t p;
  
  if(!json) return NULL;
  if(!len) len = strlen(json);
  js0n((unsigned char*)json, len, js, 64);
  if(!j0g_val("type",json,js)) return NULL;
  p = path_new(j0g_str("type",json,js));

  // just try to set all possible attributes
  path_ip(p, j0g_str("ip",json,js));
  path_port(p, (uint16_t)strtol(j0g_str("port",json,js),NULL,10));
  path_id(p, j0g_str("id",json,js));
  path_http(p, j0g_str("http",json,js));
  
  return p;
}
Пример #3
0
char *packet_get_str(packet_t p, char *key)
{
  if(!p || !key) return NULL;
  return j0g_str(key, packet_j0g(p), p->js);
}
Пример #4
0
// process a packet from HQ
void leadIncoming(const char *packet, size_t len, unsigned short *index) {
  char *type, *command, *buffer;

  buffer = (char*)malloc(len);
  memcpy(buffer, packet, len);

  uint16_t to;
  unsigned long id;

  type = j0g_str("type", buffer, index);
  if (hqVerboseOutput) {
    Serial.println(type);
  }

  if (strcmp(type, "online") == 0) {
    Shell.allReportHQ();
  }

  if (strcmp(type, "command") == 0) {
    to = atoi(j0g_str("to", buffer, index));
    id = strtoul(j0g_str("id", buffer, index), NULL, 10);
    command = j0g_str("command", buffer, index);
    if (strlen(j0g_str("to", buffer, index)) == 0 || !id || !command) {
      if (hqVerboseOutput) {
        Serial.println(F("invalid command, requires to, id, command"));
        Serial.print(F("to: "));
        Serial.println(to);
        Serial.print(F("id: "));
        Serial.println(id);
        Serial.print(F("command: "));
        Serial.println(command);
      }
      free(buffer);
      return;
    }

    // handle internal ones first
    if (to == Scout.getAddress()) {
      setOutputHandler(&printToString<&leadCommandOutput>);
      doCommand(command);
      resetOutputHandler();

      StringBuffer report;
      report.appendSprintf("{\"type\":\"reply\",\"from\":%d,\"id\":%lu,\"end\":true,\"reply\":", to, id);
      report.appendJsonString(leadCommandOutput, true);
      report += "}\n";
      leadSignal(report);
      leadCommandOutput = (char*)NULL;

      free(buffer);
      return;
    }

    // we can only send one command at a time
    if (leadCommandTo) {
      // TODO we could stop reading the HQ socket in this mode and then never get a busy?
      free(buffer);
      return leadCommandError(to,id,"busy");
    }

    // send over mesh to recipient and cache id for any replies
    leadAnswerID = id;
    leadCommandTo = to;
    leadCommandChunks = command;
    leadCommandChunksAt = 0;
    leadCommandRetries = 0;
    leadCommandChunk();
  }

  free(buffer);
}
Пример #5
0
// process a packet from HQ
void leadIncoming(char *packet, unsigned short *index) {
  char *type, *command;
  uint16_t to;
  unsigned long id;

  type = j0g_str("type", packet, index);
  if (hqVerboseOutput) {
    speol(type);
  }

  if (strcmp(type, "online") == 0) {
    Shell.allReportHQ();
  }

  if (strcmp(type, "command") == 0) {
    to = atoi(j0g_str("to", packet, index));
    id = strtoul(j0g_str("id", packet, index), NULL, 10);
    command = j0g_str("command", packet, index);
    if (strlen(j0g_str("to", packet, index)) == 0 || !id || !command) {
      if (hqVerboseOutput) {
        speol("invalid command, requires to, id, command");
        sp("to: ");
        speol(to);
        sp("id: ");
        speol(id);
        sp("command: ");
        speol(command);
      }
      return;
    }

    // handle internal ones first
    if (to == Scout.getAddress()) {
      Shell.bitlashOutput = (char*)malloc(255);

      sprintf(Shell.bitlashOutput,"{\"type\":\"reply\",\"from\":%d,\"id\":%lu,\"end\":true,\"reply\":\"", to, id);
      setOutputHandler(&bitlashBuffer);
      doCommand(command);
      strcpy(Shell.bitlashOutput + strlen(Shell.bitlashOutput), "\"}\n");
      setOutputHandler(&bitlashFilter);
      leadSignal(Shell.bitlashOutput);
      free(Shell.bitlashOutput);
      return;
    }

    // we can only send one command at a time
    if (leadCommandTo) {
      // TODO we could stop reading the HQ socket in this mode and then never get a busy?
      return leadCommandError(to,id,"busy");
    }

    // send over mesh to recipient and cache id for any replies
    leadAnswerID = id;
    leadCommandTo = to;
    leadCommandChunks = (char*)malloc(strlen(command) + 1);
    strcpy(leadCommandChunks, command);
    leadCommandChunksAt = 0;
    leadCommandRetries = 0;
    leadCommandChunk();
  }
}