예제 #1
0
파일: 32.hpp 프로젝트: hanslovsky/exercise
void get_set_of_pandigital_products(int* begin, int* end, std::set<int>& prods) {
  int n_digits = end-begin;
  int n_max = 5;
  int x1, x2, res;
  do {
    for (int i = 0; i < n_max-1; ++ i) {
      x1 = get_int_from_array(begin, begin+i);
      x2 = get_int_from_array(begin+i, begin+n_max);
      res = get_int_from_array(begin+n_max, begin+n_digits);
      if (check_product(x1, x2, res)) {
        prods.insert(res);
      }
    }
  } while (std::next_permutation(begin, end));
}
예제 #2
0
int dispatch_msg(const char* p_msg)
{
    log_debug("parsing message: %s", p_msg);
    struct json_object* pobj = json_tokener_parse(p_msg);
    if((NULL == pobj) || (is_error(pobj)))
    {
        log_err("error: message does not appear to be a valid json message: %s", p_msg);
        return(-1);
    }

    // param 0 is the function name
    const char* fcn;
    int rc = get_string_from_array(pobj, 0, &fcn);
    if(0 != rc)
    {
        log_err("error: failed to get function name from message");
        return(-1);
    }

    if(0 == strcmp("pulseRelay", fcn))
    {
        // ["pulseRelay",1,250]
        int num;
        rc = get_int_from_array(pobj, 1, &num);
        if(0 != rc)
        {
            log_err("error: pulseRelay: failed to extract param0 relay num");
            return(-1);
        }
        if((num < 0) || (num > 255))
        {
            log_err("error: pulseRelay: relay num out of range (0-255): [%d]", num);
            return(-1);
        }

        int ms;
        rc = get_int_from_array(pobj, 2, &ms);
        if(0 != rc)
        {
            log_debug("pulseRelay: no cycle ms specified, defaulting to 250ms");
            ms = 250;
        }
        if((ms < 0) || (ms > 255))
        {
            log_err("error: pulseRelay: pulse duration ms out of range (0-255): [%dms]", ms);
            return(-1);
        }

        pulseRelay((uint8_t)num, (uint8_t)ms);
    }

    else if(0 == strcmp("writeOutputRegister", fcn))
    {
        // ["writeOutputRegister",1,255]
        int val;
        rc = get_int_from_array(pobj, 1, &val);
        if(0 != rc)
        {
            log_err("error: writeOutputRegister: failed to extract param0 'value'");
            return(-1);
        }
        if((val < 0) || (val > 255))
        {
            log_err("error: writeOutputRegister: register value out of range (0-255): [%d]", val);
            return(-1);
        }

        int mask;
        rc = get_int_from_array(pobj, 2, &mask);
        if(0 != rc)
        {
            log_err("error: writeOutputRegister: failed to extract param1 'mask'");
            return(-1);
        }
        if((mask < 0) || (mask > 255))
        {
            log_err("error: writeOutputRegister: register mask out of range (0-255): [%d]", mask);
            return(-1);
        }

        writeOutputRegister((uint8_t)val, (uint8_t)mask);
    }

    else if(0 == strcmp("dialModem", fcn))
    {
        // ["dialModem","ATD3,4,4;"]
        const char* val;
        rc = get_string_from_array(pobj, 1, &val);
        if(0 != rc)
        {
            log_err("error: dialModem: failed to extract param0 dial string");
            return(-1);
        }

        dialModem(val);
    }

    else if(0 == strcmp("hello", fcn))
    {
        // ["hello","<device id>"]
        const char* str;
        rc = get_string_from_array(pobj, 1, &str);
        if(0 != rc)
        {
            log_err("error: hello: failed to extract param0 device id");
            return(-1);
        }

        processHello(str);
    }

    else if(0 == strcmp("requestIpv4Addresses", fcn))
    {
        requestIpv4Addresses();
    }

    else
    {
        log_err("error: unknown function: [%s]", fcn);
    }

    return(0);
}