示例#1
0
/*
 * Restore the previous mode (if there is one) and clear the previous mode
 */
void restore_mode(uint8_t place)
{
  set_mode(place, previous_modes[place]);
  previous_modes[place] = MODE_NONE;
  DEBUG4_VALUE("Restored mode ", place);
  DEBUG4_VALUELN("=", current_modes[place]);
}
示例#2
0
/*
 * Process a program configuration message
 */
boolean ProgramManager::handle_msg(msg_program_t *msg) {
  DEBUG4_VALUE("handle_msg: program=", msg->type);
  DEBUG4_VALUELN(" output=", msg->hdr.output);

  /* Find the program to be executed */
  hmtl_program_t *program = lookup_function(msg->type);
  if (program == NULL) {
    DEBUG1_VALUELN("handle_msg: invalid type: ",
		  msg->type);
    return false;
  }

   /* Setup the tracker */
  if (msg->hdr.output > num_outputs) {
    DEBUG1_VALUELN("handle_msg: invalid output: ",
		  msg->hdr.output);
    return false;
  }
  if (outputs[msg->hdr.output] == NULL) {
    DEBUG1_VALUELN("handle_msg: NULL output: ",
		  msg->hdr.output);
    return false;
  }

  program_tracker_t *tracker = trackers[msg->hdr.output];

  if (program->type == HMTL_PROGRAM_NONE) {
    /* This is a message to clear the existing program so free the tracker */
    free_tracker(msg->hdr.output);
    return true;
  }

  if (tracker != NULL) {
    DEBUG5_PRINTLN("handle_msg: reusing old tracker");
    if (tracker->state) {
      DEBUG5_PRINTLN("handle_msg: deleting old state");
      free(tracker->state);
    }
  } else {
    tracker = (program_tracker_t *)malloc(sizeof (program_tracker_t));
    trackers[msg->hdr.output] = tracker;
  }

  tracker->program = program;
  tracker->flags = 0x0;
  tracker->program->setup(msg, tracker);

  return true;
}
示例#3
0
/*
 * Process a command message for a particular output
 */
int
hmtl_handle_output_msg(msg_hdr_t *msg_hdr,
                       byte num_outputs, output_hdr_t *outputs[],
                       void *objects[])
{
  if (msg_hdr->type != MSG_TYPE_OUTPUT) {
    DEBUG_ERR("hmtl_handle_msg: incorrect msg type");
    return -1;
  }

  output_hdr_t *msg = (output_hdr_t *)(msg_hdr + 1);
  DEBUG4_VALUE("hmtl_handle_msg: type=", msg->type);
  DEBUG4_VALUELN(" out=", msg->output);

  if (msg->output >= num_outputs) {
    DEBUG_ERR("hmtl_handle_msg: output over max");
    return -1;
  }

  output_hdr_t *out = outputs[msg->output];
  void *data = (objects != NULL ? objects[msg->output] : NULL);

  switch (msg->type) {
    case HMTL_OUTPUT_VALUE:
      {
        msg_value_t *msg2 = (msg_value_t *)msg;
        uint8_t values[3];
        for (byte i = 0; i < 3; i++) {
          values[i] = msg2->value;
        }
        hmtl_set_output_rgb(out, data, values);
        break;
      }

    case HMTL_OUTPUT_RGB:
      {
        msg_rgb_t *msg2 = (msg_rgb_t *)msg;
        hmtl_set_output_rgb(out, data, msg2->values);
        break;
      }
    default: {
      // Unknown output type
      DEBUG_ERR("Unhandled output type");
      return -1;
    }
  }

  return 0;
}
示例#4
0
/* Process a message if it is for this module */
boolean MessageHandler::process_msg(msg_hdr_t *msg_hdr, Socket *src,
                                    Socket *serial_socket,
                                    config_hdr_t *config) {
  if (msg_hdr->version != HMTL_MSG_VERSION) {
    DEBUG_ERR("Invalid message version");
    return false;
  }

  /* Test if the message is for this device */
  if ((msg_hdr->address == address) ||
      (msg_hdr->address == SOCKET_ADDR_ANY)) {

    if ((msg_hdr->flags & MSG_FLAG_ACK) &&
        (msg_hdr->address != SOCKET_ADDR_ANY)) {
      /*
       * This is an ack message that is not for us, resend it over serial in
       * case that was the original source.
       * TODO: Maybe this should check address as well, and serial needs to be
       * assigned an address?
       */
      DEBUG4_PRINTLN("Forwarding ack to serial");
      Serial.write((byte *)msg_hdr, msg_hdr->length);

      if (msg_hdr->type != MSG_TYPE_SENSOR) { // Sensor broadcasts are for everyone
        return false;
      }
    }

    switch (msg_hdr->type) {

      case MSG_TYPE_OUTPUT: {
        output_hdr_t *out_hdr = (output_hdr_t *)(msg_hdr + 1);
        if (out_hdr->type == HMTL_OUTPUT_PROGRAM) {
          manager->handle_msg((msg_program_t *)out_hdr);
        } else {
          hmtl_handle_output_msg(msg_hdr, manager->num_outputs,
                                 manager->outputs, manager->objects);
        }

        return true;
      }

      case MSG_TYPE_POLL: {
        // Generate a response to a poll message
        uint16_t source_address = 0;
        Socket *sock;

        if (src != NULL) {
          // The response will be going over a socket, get the source address
          source_address = src->sourceFromData(msg_hdr);
          sock = src;
        } else {
          // The data will be sent back to the indicated Serial device.  A
          // socket still needs to be specified in order to have a buffer to
          // fill.
          sock = serial_socket;
        }

        DEBUG3_VALUELN("Poll req src:", source_address);

        // Format the poll response
        uint16_t len = hmtl_poll_fmt(sock->send_buffer,
                                     sock->send_data_size,
                                     source_address,
                                     msg_hdr->flags, OBJECT_TYPE,
                                     config,
                                     manager->outputs,
                                     sock->recvLimit);

        // Respond to the appropriate source
        if (src != NULL) {
          if (msg_hdr->address == SOCKET_ADDR_ANY) {
            // If this was a broadcast address then do not respond immediately,
            // delay for time based on our address.
            int delayMs = address * 2;
            DEBUG3_VALUELN("Delay resp: ", delayMs)
            delay(delayMs); // TODO: This blocks any running program?  Use a timer
          }

          src->sendMsgTo(source_address, sock->send_buffer, len);
        } else {
          // Send the response on the serial device
          Serial.write(sock->send_buffer, len);
        }

        break;
      }

      case MSG_TYPE_SET_ADDR: {
        /* Handle an address change message */
        msg_set_addr_t *set_addr = (msg_set_addr_t *)(msg_hdr + 1);
        if ((set_addr->device_id == 0) ||
            (set_addr->device_id == config->device_id)) {
          address = set_addr->address;
          src->sourceAddress = address;
          DEBUG2_VALUELN("Address changed to ", address);
        }
        break;
      }

      case MSG_TYPE_SENSOR: {
        if (msg_hdr->flags & MSG_FLAG_ACK) {
          /*
           * This is a sensor response, record relevant values for usage
           * elsewhere.
           */
          msg_sensor_data_t *sensor = NULL;
          while ((sensor = hmtl_next_sensor(msg_hdr, sensor))) {
            // Call the ProgramManager's handler for the sensor function
            manager->run_program(PROGRAM_SENSOR_DATA, sensor);
          }
          DEBUG_PRINT_END();
        }
        break;
      }

      case MSG_TYPE_TIMESYNC: {
        /*
         * This is a time synchronization message, send to the ProgramManager's
         * TimeSync object.
         */
        timesync.synchronize(src, SOCKET_ADDR_INVALID, msg_hdr);
        break;
      }

      case MSG_TYPE_DUMP_CONFIG: {
        /*
         * This is a request to dump the EEPROM objects to the serial device.
         *
         * TODO: This could be made to work remotely rather than only to requests
         * from the serial device.
         */
        uint16_t source_address = 0;

        DEBUG3_VALUELN("Dump req src:", source_address);

        /*
         * The response will be a typical message header followed by the raw data
         * from EEPROM.
         */
        msg_dumpconfig_response_t *resp = (msg_dumpconfig_response_t *)(serial_socket->send_buffer + sizeof (msg_hdr_t));

        int location = HMTL_CONFIG_ADDR; // Starting location in EEPROM
        int next_addr = 0;
        uint8_t flags;

        do {
          uint16_t len;
          flags = msg_hdr->flags;

          next_addr = EEPROM_safe_read(location, resp->data,
                                       serial_socket->send_data_size -
                                           HMTL_MSG_DUMPCONFIG_MIN_LEN);
          if (next_addr > 0) {
            uint16_t datalen = (uint16_t)EEPROM_DATA_SIZE(next_addr - location);

            DEBUG4_VALUELN("Dump config:", location);

            /*
             * Check if the next address is a valid structure and if so indicate
             * that there will be additional messages.
             */
            if (EEPROM_check_address(next_addr)) {
              flags |= MSG_FLAG_MORE_DATA;
            } else {
              DEBUG4_PRINTLN("Dump final message")
            }

            // Now that the length of the data is known construct the message
            len = hmtl_dumpconfig_fmt(serial_socket->send_buffer,
                                      serial_socket->send_data_size,
                                      source_address,
                                      flags,
                                      datalen);

            location = next_addr;
          } else {
            /*
             * There was an error, respond with an error flag
             */
            flags |= MSG_FLAG_ERROR;

            len = hmtl_dumpconfig_fmt(serial_socket->send_buffer,
                                      serial_socket->send_data_size,
                                      source_address,
                                      flags,
                                      0);
          }

          Serial.write(serial_socket->send_buffer, len);

        } while (flags & MSG_FLAG_MORE_DATA);

        break;
      }
    }
  }
示例#5
0
/*
 * Process a program configuration message
 */
boolean ProgramManager::handle_msg(msg_program_t *msg) {
  DEBUG4_VALUE("handle_msg: program=", msg->type);
  DEBUG4_VALUELN(" output=", msg->hdr.output);

  /* Find the program to be executed */
  byte program = lookup_function(msg->type);
  if (program == NO_PROGRAM) {
    DEBUG1_VALUELN("handle_msg: invalid type: ", msg->type);
    return false;
  }

  /* Setup the tracker */
  int starting_output, stop_output;

  if (msg->hdr.output == HMTL_ALL_OUTPUTS) {
    /* This should be applied to all outputs that can handle the message type */
    starting_output = 0;
    stop_output = num_outputs;
  } else if (msg->hdr.output > num_outputs) {
    DEBUG1_VALUELN("handle_msg: invalid output: ",
                   msg->hdr.output);
    return false;
  } else if (outputs[msg->hdr.output] == NULL) {
    DEBUG1_VALUELN("handle_msg: NULL output: ", msg->hdr.output);
    return false;
  } else {
    /* Only apply to the specified output */
    starting_output = msg->hdr.output;
    stop_output = starting_output + 1;
  }

  for (int output = starting_output; output < stop_output; output++) {

    if (outputs[output] == NULL)
      continue;

    if (msg->type == HMTL_PROGRAM_NONE) {
      /* This is a message to clear the existing program so free the tracker */
      DEBUG3_VALUELN("handle_msg: clear ", output);
      free_tracker(output);
      continue;
    }

    program_tracker_t *tracker;
    if (functions[program].program == NULL) {
      /*
       * This is an initialization-only command, set tracker to null
       */
      DEBUG4_PRINTLN("handle_msg: trackerless")
      tracker = NULL;
    } else {
      /* If there was an active program on this output then clear the tracker */
      free_tracker(output);

      /* Setup a tracker for this program */
      tracker = get_tracker(output);
      tracker->program_index = program;
      tracker->flags = 0x0;
    }

    if (tracker) {
      // Record the output and object in the tracker
      tracker->output = outputs[output];
      tracker->object = objects[output];
    }

    /* Attempt to setup the program */


    boolean success = functions[program].setup(msg, tracker, outputs[output],
                                               objects[output], this);

    if (!success) {
      if (tracker) {
        DEBUG4_VALUELN("handle_msg: NA on ", output);
        free_tracker(output);
      }
      continue;
    }
    DEBUG4_VALUELN("handle_msg: setup on ", output);
  }

  return true;
}