/*********************************************************************
 * Function:        void SYS_ArduinoMonitorTask(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          Receive and monitor communications with the Arduino-shield serial comms channel
 *
 * Side Effects:    None
 *
 * Overview:        This function implements the task for monitoring comms from the Arduino shield.
 *
 * Note:            None
 ********************************************************************/
void SYS_ArduinoMonitorTask(FlowThread thread, void *taskParameters)
{
	char incomingChar = 0;

	#define ARDUINO_SERIAL_PORT 6
	#define ARDUINO_SERIAL_BAUD 115200

	// power the PMODRS232
	#if defined(MICROCHIP_PIC32)
    /*
     * UART 6 - Shield RS232 port
     * - u6Txd on pin RPB15
     * - u6RXd on pin RPC2
     */

    // TXd Pin setup.
	PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_15, PORTS_PIN_MODE_DIGITAL);
    PLIB_PORTS_PinDirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_B, PORTS_BIT_POS_15 );
    RPB15R = 0x04;

    // RXd Pin setup.
	PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_2, PORTS_PIN_MODE_DIGITAL);
	PLIB_PORTS_PinDirectionInputSet( PORTS_ID_0, PORT_CHANNEL_C, PORTS_BIT_POS_2 );
    U6RXR = 0x0C;

	// RPG8 3V3 supply Digilent PMODRS232
    PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_8, PORTS_PIN_MODE_DIGITAL);
	PLIB_PORTS_PinDirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_8 );
	PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_8 );

	// RPG7 0V supply to Digilent PMODRS232
    PLIB_PORTS_PinModePerPortSelect (PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7, PORTS_PIN_MODE_DIGITAL);
	PLIB_PORTS_PinDirectionOutputSet( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7 );
	PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_G, PORTS_BIT_POS_7 );
	#endif

	arduinoSerial = FlowSerial_Init(ARDUINO_SERIAL_PORT, ARDUINO_SERIAL_BAUD);

	#define ARDUINO_CMD_BUFFER_LENGTH 255
	char commandBuffer[ARDUINO_CMD_BUFFER_LENGTH+1];
	char* cursor = commandBuffer;
	unsigned int freeBufferSize = ARDUINO_CMD_BUFFER_LENGTH;
	bool processCommand = false;

	memset(commandBuffer, '\0', ARDUINO_CMD_BUFFER_LENGTH+1);

	while(1)
	{
		while (FlowSerial_Ready(arduinoSerial))
		{
			incomingChar = FlowSerial_Getc(arduinoSerial);

			if (freeBufferSize > 0 && incomingChar != '\n' && incomingChar != '\r')
			{
				*cursor = incomingChar;
				freeBufferSize--;
				cursor++;
			}
			else
			{
				*cursor = '\0';
				processCommand = true;
			}

			if (processCommand)
			{
				cmdRequestID++;
				
				// Create a request to send to device owner
				StringBuilder request = StringBuilder_New(256);

				// Response Header
				request = StringBuilder_Append(request, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
				request = StringBuilder_Append(request, "<command>");

				char* datetime = CommandHandlers_GetTimeString();
				request = StringBuilder_Append(request, "<sent type=\"datetime\">");
				request = StringBuilder_Append(request, datetime);
				request = StringBuilder_Append(request, "</sent>");
				Flow_MemFree((void **) &datetime);

				request = StringBuilder_Append(request, "<to>");
				request = StringBuilder_Append(request, g_OwnerAoR);
				request = StringBuilder_Append(request, "</to>");

				request = StringBuilder_Append(request, "<from>");
				request = StringBuilder_Append(request, g_DeviceAoR);
				request = StringBuilder_Append(request, "</from>");

				request = StringBuilder_Append(request, "<clientid type=\"integer\">");
				request = StringBuilder_Append(request, g_ClientID);
				request = StringBuilder_Append(request, "</clientid>");

				request = StringBuilder_Append(request, "<requestid type=\"integer\">");
				request = StringBuilder_AppendInt(request, cmdRequestID);
				request = StringBuilder_Append(request, "</requestid>");

				request = StringBuilder_Append(request, "<details>MIF DISPLAY ");
				request = StringBuilder_Append(request, commandBuffer);
				request = StringBuilder_Append(request, "</details>");
				
				request = StringBuilder_Append(request, "</command>");

				if (FlowMessaging_SendMessageToUser((FlowID) g_OwnerID, "text/plain", (char *)StringBuilder_GetCString(request), StringBuilder_GetLength(request), 60))
				{
					FlowConsole_Printf("\n\rSuccessfully forwarded command received from Arduino-shield comms interface ('%s').\n\r", commandBuffer);
				}
				else
				{
					FlowConsole_Puts("\n\rWarning: Could not forward command received from Arduino-shield comms interface.");
				}				

				// Clean up
				StringBuilder_Free(&request);
				processCommand = false;
				cursor = commandBuffer;
				freeBufferSize = ARDUINO_CMD_BUFFER_LENGTH;
			}
		}
        vTaskDelay( 100 / portTICK_RATE_MS );
	}
}
static bool CommandBoardDetails(int argc, char** argv)
{
    bool result = false;
    if (ConfigStore_Config_Read() && ConfigStore_Config_IsValid() && ConfigStore_Config_IsMagicValid())
    {
        StringBuilder response = StringBuilder_New(256);

        // DeviceType
        response = StringBuilder_Append(response, ConfigStore_GetDeviceType());
        response = StringBuilder_Append(response, LINE_TERM);

        // MAC address
        response = StringBuilder_Append(response, ConfigStore_GetMacAddress());
        response = StringBuilder_Append(response, LINE_TERM);

        // Serial number
        char snString[17];
        memset((void*) snString, 0, 17);
        if (DeviceSerial_GetCpuSerialNumberHexString(snString, 17))
            response = StringBuilder_Append(response, snString);
        else
            response = StringBuilder_Append(response, " ");
        response = StringBuilder_Append(response, LINE_TERM);
        // WiFi SoftAP SSID
        response = StringBuilder_Append(response, ConfigStore_GetSoftAPSSID());
        response = StringBuilder_Append(response, LINE_TERM);

        // WiFi SoftAP password
        response = StringBuilder_Append(response, ConfigStore_GetSoftAPPassword());
        response = StringBuilder_Append(response, LINE_TERM);
        char CB[2] =
        { 0, 0 };
        int32_t byteCount = StringBuilder_GetLength(response);
        int32_t byteIndex = 0;
        for (byteIndex = 0; byteIndex < byteCount; byteIndex++)
        {
            CB[0] += (StringBuilder_GetCString(response))[byteIndex];
        }
        // Compute checkbyte - 2's compliment of MOD-256 sum
        CB[0] ^= 0xFF;
        CB[0]++;
        response = StringBuilder_Append(response, CB);
        response = StringBuilder_Append(response, LINE_TERM);
        CreatorConsole_Puts(response);

        // Output entire response  // TODO - delete?
//		byteCount = StringBuilder_GetLength(response);
//		uint8_t* _response = (uint8_t*) StringBuilder_GetCString(response);
//		for (byteIndex = 0; byteIndex < byteCount; byteIndex++)
//		{
//			if (((char *) _response)[byteIndex] == '\n')
//				CreatorConsole_Puts(LINE_TERM);
//			else
//				CreatorConsole_Putc(((char *) _response)[byteIndex]);
//		}
        //CreatorConsole_Puts(LINE_TERM);

        StringBuilder_Free(&response);
        result = true;
    }
    return result;
}
示例#3
0
Int Lexer_ParseReal(Lexer lexer, Bool isFirstContentOnLine)
{
	DECLARE_INLINE_STRINGBUILDER(digitBuilder, 256);	// 256 is plenty for most numbers, but it can grow if necessary.
	const Byte *src = lexer->src;
	const Byte *end = lexer->end;
	const Byte *start;
	Byte ch;
	Token token = lexer->token;
	Int integerDigitCount = 0;
	Int fractionalDigitCount = 0;
	const Byte *digits;
	String digitString, suffix;
	const Byte *suffixText;
	Float64 float64;

	UNUSED(isFirstContentOnLine);

	INIT_INLINE_STRINGBUILDER(digitBuilder);

	START_TOKEN(src);

	// Collect integer digits.
	start = src;
	while (src < end && (ch = *src) >= '0' && ch <= '9') {
		src++;
		if (src + 1 < end
			&& ((ch = *src) == '\'' || ch == '\"' || ch == '_')
			&& src[1] >= '0' && src[1] <= '9') {
			if (src > start) {
				StringBuilder_Append(digitBuilder, start, 0, src - start);
			}
			src++;
			start = src;
		}
	}

	// Copy into the digitBuilder whatever integers are left.
	if (src > start) {
		StringBuilder_Append(digitBuilder, start, 0, src - start);
	}

	integerDigitCount = StringBuilder_GetLength(digitBuilder);

	// Collect the decimal point.
	if (src < end && *src == '.') {
		src++;

		// Collect fractional digits.
		while (src < end && (ch = *src) >= '0' && ch <= '9') {
			src++;
			if (src + 1 < end
				&& ((ch = *src) == '\'' || ch == '\"' || ch == '_')
				&& src[1] >= '0' && src[1] <= '9') {
				if (src > start) {
					StringBuilder_Append(digitBuilder, start, 0, src - start);
				}
				src++;
				start = src;
			}
		}

		fractionalDigitCount = StringBuilder_GetLength(digitBuilder) - 1 - integerDigitCount;
	}

	// Finally copy into the digitBuilder whatever's left.
	if (src > start) {
		StringBuilder_Append(digitBuilder, start, 0, src - start);
	}
	lexer->src = src;

	// Make the result C-friendly.
	StringBuilder_AppendByte(digitBuilder, '\0');

	// Extract out the raw text of the number.
	digitString = StringBuilder_ToString(digitBuilder);
	digits = String_GetBytes(digitString);

	// Get any trailing type identifiers.
	suffix = CollectAlphanumericSuffix(lexer);

	// And make sure the result is clean.
	if (!EnsureEndOfNumber(lexer)) {
		token->text = IllegalRealValueMessage;
		return END_TOKEN(TOKEN_ERROR);
	}

	suffixText = String_GetBytes(suffix);
	if (suffixText[0] == '\0') {
		// Real64.
		if (!Real64_TryParse(digitString, &token->data.real64)) {
			token->text = IllegalRealValueMessage;
			return END_TOKEN(TOKEN_ERROR);
		}
		token->text = digitString;
		return END_TOKEN(TOKEN_REAL64);
	}
	else if (suffixText[0] == 'F' || suffixText[0] == 'f') {
		if (suffixText[1] == '\0') {
			// Float64.
			float64 = strtod(digits, NULL);
			token->data.float64 = float64;
			token->text = digitString;
			return END_TOKEN(TOKEN_FLOAT64);
		}
		else goto badSuffix;
	}
	else if (suffixText[0] == 'L' || suffixText[0] == 'l') {
		// 128-bit something-or-other.
		if (suffixText[1] == '\0') {
			// Real128.
			if (!Real128_TryParse(digitString, &token->data.real128)) {
				token->text = IllegalRealValueMessage;
				return END_TOKEN(TOKEN_ERROR);
			}
			token->text = String_Concat(digitString, suffix);
			return END_TOKEN(TOKEN_REAL128);
		}
		else if ((suffixText[1] == 'F' || suffixText[1] == 'f') && suffixText[2] == '\0') {
			// Float128 (not yet supported).
			goto badSuffix;
		}
		else goto badSuffix;
	}
	else if (suffixText[0] == 'H' || suffixText[0] == 'h') {
		// 32-bit something-or-other.
		if (suffixText[1] == '\0') {
			// Real32.
			if (!Real32_TryParse(digitString, &token->data.real32)) {
				token->text = IllegalRealValueMessage;
				return END_TOKEN(TOKEN_ERROR);
			}
			token->text = String_Concat(digitString, suffix);
			return END_TOKEN(TOKEN_REAL32);
		}
		else if ((suffixText[1] == 'F' || suffixText[1] == 'f') && suffixText[2] == '\0') {
			// Float32.
			float64 = strtod(digits, NULL);
			token->data.float32 = (Float32)float64;
			token->text = digitString;
			return END_TOKEN(TOKEN_FLOAT32);
		}
		else goto badSuffix;
	}
	else goto badSuffix;

badSuffix:
	token->text = String_FormatString(IllegalNumericSuffixMessage, suffix);
	return END_TOKEN(TOKEN_ERROR);
}