Esempio n. 1
0
int8_t CoAP_FindUriQueryVal(CoAP_option_t* pUriOpt, const char* prefixStr, int CmpStrCnt, ...) {
	va_list ap; //compare string pointer
	int i,j;
	char* pStr=NULL;
	bool Match=false;
	uint8_t* pUriQueryVal;
	uint8_t ValLen;

	pUriQueryVal = CoAP_GetUriQueryVal(pUriOpt, prefixStr, &ValLen);
	if(pUriQueryVal == NULL) return 0;//-1; //prefix not found, no uri-query

	va_start (ap, CmpStrCnt);         // Initialize the argument list.
	for(i=1;i<CmpStrCnt+1;i++) { //loop over all string arguments to compare the found uri query against
		pStr = va_arg(ap, char*);

		if(coap_strlen(pStr) != ValLen) continue; //already length does not match -> try next given string

		Match=true;
		for(j=0;j< ValLen; j++) {
			if(pStr[j] != pUriQueryVal[j]){
				Match=false;
				break;
			}
		}
		if(Match==false) continue;
		//found argument string matching to uri-query value
		va_end (ap);
		return i; //return argument number of match
	}

	 va_end (ap);
	 return 0; //not found
}
Esempio n. 2
0
static CoAP_HandlerResult_t ICACHE_FLASH_ATTR Res_ReqHandler(CoAP_Message_t* pReq, CoAP_Message_t* pResp) {
	if(pReq->Code == REQ_POST) {
		CoAP_option_t* pOpt;
		bool Found = false;

		for(pOpt =pReq->pOptionsList ; pOpt != NULL; pOpt = pOpt->next) {
			switch(CoAP_FindUriQueryVal(pOpt,"",3, "on","off", "tgl")) { //no prefix used -> use /led_gpio12?on or /led_gpio12?off
				case 0: break; //not found
				case 1: led(true); Found=true; break; //found "on"
				case 2: led(false); Found=true; break; //found "off"
				case 3: led(!LedState); Found=true; break; //found "tgl"
			}
			if(Found) {
				SetLedstatePayload(pReq, pResp);
				break;
			}
		}

		if(!Found){
			char info[] = {"usage: coap://.../led_gpio12?on (or \"off\", \"tgl\")"};
			CoAP_SetPayload(pReq, pResp, info, coap_strlen(info), true);
			pResp->Code=RESP_ERROR_BAD_REQUEST_4_00;
		}

	}else if(pReq->Code == REQ_GET){
		SetLedstatePayload(pReq, pResp);
	}

	return HANDLER_OK;
}
Esempio n. 3
0
static CoAP_HandlerResult_t ICACHE_FLASH_ATTR NotifyHandler(CoAP_Observer_t* pObserver, CoAP_Message_t* pResp) {
	char myString[20];
	coap_sprintf(myString,"%d [s]", hal_rtc_1Hz_Cnt());
	CoAP_SetPayload(NULL, pResp, myString, coap_strlen(myString), true);

	return HANDLER_OK;
}
Esempio n. 4
0
uint8_t* CoAP_GetUriQueryVal(CoAP_option_t* pUriOpt, const char* prefixStr, uint8_t* pValueLen){
	if(pUriOpt == NULL) return NULL;
	if(pUriOpt->Number != OPT_NUM_URI_QUERY) return NULL;
	if(pUriOpt->Length == 0 || pUriOpt->Length > 255) return NULL;

	int prefixLen = coap_strlen(prefixStr);
	if(prefixLen >= pUriOpt->Length) return NULL;

	int i=0;
	for(;i< prefixLen; i++) {
		if(pUriOpt->Value[i] != prefixStr[i]) return NULL;
	}

	//prefix found
	if(pValueLen != NULL)
		*pValueLen = (pUriOpt->Length) - prefixLen;

	return &(pUriOpt->Value[prefixLen]);
}
Esempio n. 5
0
static void ICACHE_FLASH_ATTR SetLedstatePayload(CoAP_Message_t* pReq, CoAP_Message_t* pResp){
	if(LedState) CoAP_SetPayload(pReq, pResp, "Led is on!", coap_strlen("Led is on!"), true);
	else CoAP_SetPayload(pReq, pResp, "Led is off!", coap_strlen("Led is off!"), true);
}