예제 #1
0
파일: tm_timer.c 프로젝트: Gozhack/runtime
/// Create a timer and enqueue it
unsigned tm_settimeout(unsigned time, bool repeat, int lua_cb) {
	tm_timer* t = calloc(sizeof(tm_timer), 1);
	t->repeat = repeat ? time : 0;
	t->lua_cb = lua_cb;
	t->next = 0;
	t->id = ++timer_id;

	if (!timers_head) {
		last_time = tm_uptime_micro();
	} else {
		// Adjust because the times on the queue are relative to last_time
		time += (tm_uptime_micro() - last_time);
	}

	if (enqueue_timer(time, t)) {
		configure_timer_interrupt();
	}

	return t->id;
}
예제 #2
0
파일: tm_random.c 프로젝트: Olegas/firmware
unsigned px_acquire_system_randomness(uint8_t *buf)
{

	for (int i=0; i<RND_BYTES; i+=4) {
		buf[i]   = (uint8_t) tm_uptime_micro();
		buf[i+1] = (uint8_t) hw_analog_read(ADC_5);
		buf[i+2] = (uint8_t) hw_analog_read(ADC_7);
		buf[i+3] = (uint8_t) hw_analog_read(E_A1);
	}

	return RND_BYTES;
}
예제 #3
0
파일: tm_timer.c 프로젝트: Gozhack/runtime
/// Callback enqueued by the timer ISR. It is safe for this to be called more
/// often than necessary
void timer_cb(tm_event* event) {
	(void) event;
	
	unsigned prev_time = last_time;
	last_time = tm_uptime_micro();
	unsigned elapsed = last_time - prev_time;

	while (timers_head) {
		tm_timer* t = timers_head;

		if (t->time > elapsed) {
			t->time -= elapsed;
			break;
		}

		elapsed -= t->time;
		timers_head = t->next;

		lua_rawgeti(tm_lua_state, LUA_REGISTRYINDEX, t->lua_cb);

		if (t->repeat != 0) {
			// It's back in the queue, so clearInterval within the callback can cancel it
			enqueue_timer(t->repeat, t);
		} else {
			// Clean up before calling lua, as it can setjmp. The callback is safely rooted on the lua stack above.
			destroy_timer(t);
			t = NULL;
		}

		lua_getfield(tm_lua_state, LUA_GLOBALSINDEX, "global");
		tm_checked_call(tm_lua_state, 1);
	}

	// If lua setjmps, these will be cleaned up when the timer state is reset
	configure_timer_interrupt();
}
예제 #4
0
unsigned char *
hci_event_handler(void *pRetParams, unsigned char *from, unsigned char *fromlen)
{
	renableIRQ();
//	TM_DEBUG("hci event handler");
//	TM_DEBUG("event or data recieved y/n? %ul", tSLInformation.usEventOrDataReceived);
//	TM_DEBUG("the actual event/data %ul", (unsigned char *)tSLInformation.pucReceivedData[0]);
	volatile unsigned char *pucReceivedData, ucArgsize;
	unsigned short usLength;
	unsigned char *pucReceivedParams;
	unsigned short usReceivedEventOpcode = 0;
	unsigned long retValue32;
	unsigned char * RecvParams;
	unsigned char *RetParams;

#ifdef CC3K_TIMEOUT
	uint32_t ccStartTime = tm_uptime_micro();
#endif

	while (1)
	{
		CC_BLOCKS();

		if (tSLInformation.usEventOrDataReceived != 0)
		{

			pucReceivedData = (tSLInformation.pucReceivedData);

			if (*pucReceivedData == HCI_TYPE_EVNT)
			{
				// Event Received
				STREAM_TO_UINT16((char *)pucReceivedData, HCI_EVENT_OPCODE_OFFSET,
												 usReceivedEventOpcode);
				pucReceivedParams = (unsigned char*) (pucReceivedData + HCI_EVENT_HEADER_SIZE);
				RecvParams = pucReceivedParams;
				RetParams = pRetParams;
//				TM_DEBUG("PUC RECIEVED DATA pre %ul", (char *)pucReceivedData[0]);
				// In case unsolicited event received - here the handling finished
				if (hci_unsol_event_handler((char *)pucReceivedData) == 0)
				{
					STREAM_TO_UINT8(pucReceivedData, HCI_DATA_LENGTH_OFFSET, usLength);
//					TM_DEBUG("PUC RECIEVED DATA %ul",(char *) pucReceivedData[0]);
					switch(usReceivedEventOpcode)
					{		
					case HCI_CMND_READ_BUFFER_SIZE:
						{
							STREAM_TO_UINT8((char *)pucReceivedParams, 0, 
															tSLInformation.usNumberOfFreeBuffers);
							STREAM_TO_UINT16((char *)pucReceivedParams, 1, 
															 tSLInformation.usSlBufferLength);
						}
						break;
						
					case HCI_CMND_WLAN_CONFIGURE_PATCH:
					case HCI_NETAPP_DHCP:
					case HCI_NETAPP_PING_SEND:
					case HCI_NETAPP_PING_STOP:
					case HCI_NETAPP_ARP_FLUSH:
					case HCI_NETAPP_SET_DEBUG_LEVEL:
					case HCI_NETAPP_SET_TIMERS:
					case HCI_EVNT_NVMEM_READ:
					case HCI_EVNT_NVMEM_CREATE_ENTRY:
					case HCI_CMND_NVMEM_WRITE_PATCH:
					case HCI_NETAPP_PING_REPORT:
					case HCI_EVNT_MDNS_ADVERTISE:
						
						STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET
														,*(unsigned char *)pRetParams);
						break;
						
					case HCI_CMND_SETSOCKOPT:
					case HCI_CMND_WLAN_CONNECT:
					case HCI_CMND_WLAN_IOCTL_STATUSGET:
					case HCI_EVNT_WLAN_IOCTL_ADD_PROFILE:
					case HCI_CMND_WLAN_IOCTL_DEL_PROFILE:
					case HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY:
					case HCI_CMND_WLAN_IOCTL_SET_SCANPARAM:
					case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START:
					case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP:
					case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX:
					case HCI_CMND_EVENT_MASK:
					case HCI_EVNT_WLAN_DISCONNECT:
					case HCI_EVNT_SOCKET:
					case HCI_EVNT_BIND:
					case HCI_CMND_LISTEN:
					case HCI_EVNT_CLOSE_SOCKET:
					case HCI_EVNT_CONNECT:
					case HCI_EVNT_NVMEM_WRITE:
						
						STREAM_TO_UINT32((char *)pucReceivedParams,0
														 ,*(unsigned long *)pRetParams);
						break;
						
					case HCI_EVNT_READ_SP_VERSION:
						
						STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET
														,*(unsigned char *)pRetParams);
						pRetParams = ((char *)pRetParams) + 1;
						STREAM_TO_UINT32((char *)pucReceivedParams, 0, retValue32);
						UINT32_TO_STREAM((unsigned char *)pRetParams, retValue32);				
						break;
						
					case HCI_EVNT_BSD_GETHOSTBYNAME:
						
						STREAM_TO_UINT32((char *)pucReceivedParams
						      ,GET_HOST_BY_NAME_RETVAL_OFFSET,*(unsigned long *)pRetParams);
						pRetParams = ((char *)pRetParams) + 4;
						STREAM_TO_UINT32((char *)pucReceivedParams
									,GET_HOST_BY_NAME_ADDR_OFFSET,*(unsigned long *)pRetParams);					
						break;
						
					case HCI_EVNT_ACCEPT:
						{
							STREAM_TO_UINT32((char *)pucReceivedParams,ACCEPT_SD_OFFSET
															 ,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams
										,ACCEPT_RETURN_STATUS_OFFSET,*(unsigned long *)pRetParams);
              pRetParams = ((char *)pRetParams) + 4; 
							
							//This argument returns in network order
							memcpy((unsigned char *)pRetParams, 
								  pucReceivedParams + ACCEPT_ADDRESS__OFFSET, sizeof(sockaddr));	
							break;
						}
						
					case HCI_EVNT_RECV:
					case HCI_EVNT_RECVFROM:
						{
							STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE__FLAGS__OFFSET,*(unsigned long *)pRetParams);
							
							if(((tBsdReadReturnParams *)pRetParams)->iNumberOfBytes == ERROR_SOCKET_INACTIVE)
							{
								set_socket_active_status(((tBsdReadReturnParams *)pRetParams)->iSocketDescriptor,SOCKET_STATUS_INACTIVE);
							}
							break;
						}
                                                
                                        case HCI_EVNT_SEND:
					case HCI_EVNT_SENDTO:
						{
							STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							
							break;
						}
						
					case HCI_EVNT_SELECT:
						{ 
							STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_STATUS_OFFSET,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_READFD_OFFSET,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_WRITEFD_OFFSET,*(unsigned long *)pRetParams);
							pRetParams = ((char *)pRetParams) + 4;
							STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_EXFD_OFFSET,*(unsigned long *)pRetParams);			
							break;
						}
						
					case HCI_CMND_GETSOCKOPT:
						
						STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET,((tBsdGetSockOptReturnParams *)pRetParams)->iStatus);
						//This argument returns in network order
						memcpy((unsigned char *)pRetParams, pucReceivedParams, 4);
						break;
						
					case HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS:
						
						STREAM_TO_UINT32((char *)pucReceivedParams,GET_SCAN_RESULTS_TABlE_COUNT_OFFSET,*(unsigned long *)pRetParams);
						pRetParams = ((char *)pRetParams) + 4;   					
						STREAM_TO_UINT32((char *)pucReceivedParams,GET_SCAN_RESULTS_SCANRESULT_STATUS_OFFSET,*(unsigned long *)pRetParams);
						pRetParams = ((char *)pRetParams) + 4;                                                        					
						STREAM_TO_UINT16((char *)pucReceivedParams,GET_SCAN_RESULTS_ISVALID_TO_SSIDLEN_OFFSET,*(unsigned long *)pRetParams);
						pRetParams = ((char *)pRetParams) + 2;   					
						STREAM_TO_UINT16((char *)pucReceivedParams,GET_SCAN_RESULTS_FRAME_TIME_OFFSET,*(unsigned long *)pRetParams);
						pRetParams = ((char *)pRetParams) + 2;  
						memcpy((unsigned char *)pRetParams, (char *)(pucReceivedParams + GET_SCAN_RESULTS_FRAME_TIME_OFFSET + 2), GET_SCAN_RESULTS_SSID_MAC_LENGTH);	
						break;
						
					case HCI_CMND_SIMPLE_LINK_START:
						break;
						
					case HCI_NETAPP_IPCONFIG:
						
						//Read IP address
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
						RecvParams += 4;
						
						//Read subnet
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
						RecvParams += 4;
						
						//Read default GW
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
						RecvParams += 4;
						
						//Read DHCP server                                          	
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
						RecvParams += 4;
						
						//Read DNS server                                           
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
						RecvParams += 4;
						
						//Read Mac address                            	
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_MAC_LENGTH);
						RecvParams += 6;
						
						//Read SSID
						STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_SSID_LENGTH);
	
					}
				}
				
				if (usReceivedEventOpcode == tSLInformation.usRxEventOpcode)
				{
					tSLInformation.usRxEventOpcode = 0;
				}
			}
			else
			{				
				pucReceivedParams = (unsigned char*) pucReceivedData;
				STREAM_TO_UINT8((char *)pucReceivedData, HCI_PACKET_ARGSIZE_OFFSET, ucArgsize);
				
				STREAM_TO_UINT16((char *)pucReceivedData, HCI_PACKET_LENGTH_OFFSET, usLength);

				// Data received: note that the only case where from and from length 
				// are not null is in recv from, so fill the args accordingly
				if (from)
				{
					STREAM_TO_UINT32((char *)(pucReceivedData + HCI_DATA_HEADER_SIZE), BSD_RECV_FROM_FROMLEN_OFFSET, *(unsigned long *)fromlen);
					memcpy(from, (void*) (pucReceivedData + HCI_DATA_HEADER_SIZE + BSD_RECV_FROM_FROM_OFFSET) ,*fromlen);
				}
				
				memcpy(pRetParams, pucReceivedParams + HCI_DATA_HEADER_SIZE + ucArgsize,
							 usLength - ucArgsize);
				
				tSLInformation.usRxDataPending = 0;
			}
		
			tSLInformation.usEventOrDataReceived = 0;
			
			SpiResumeSpi();
			
			// Since we are going to TX - we need to handle this event after the 
			// ResumeSPi since we need interrupts
			if ((*pucReceivedData == HCI_TYPE_EVNT) &&
					(usReceivedEventOpcode == HCI_EVNT_PATCHES_REQ))
			{
				hci_unsol_handle_patch_request((char *)pucReceivedData);
			}
			
			if ((tSLInformation.usRxEventOpcode == 0) && (tSLInformation.usRxDataPending == 0))
			{
				return NULL;
			}	
		} 
#ifdef CC3K_TIMEOUT

		else {

			// check the system timer
			// compare it to maximum allowed wait
			if (tm_uptime_micro() - ccStartTime >= CC3000_MAX_WAIT) {

				// set pRetParams to some default values
				switch(tSLInformation.usRxEventOpcode) {
					case HCI_CMND_READ_BUFFER_SIZE:
						// tSLInformation values are defaulted to 0
						tSLInformation.usNumberOfFreeBuffers = 0;
						tSLInformation.usSlBufferLength = 0;
						break;
					case HCI_CMND_WLAN_CONFIGURE_PATCH:
					case HCI_NETAPP_DHCP:
					case HCI_NETAPP_PING_SEND:
					case HCI_NETAPP_PING_STOP:
					case HCI_NETAPP_ARP_FLUSH:
					case HCI_NETAPP_SET_DEBUG_LEVEL:
					case HCI_NETAPP_SET_TIMERS:
					case HCI_EVNT_NVMEM_READ:
					case HCI_EVNT_NVMEM_CREATE_ENTRY:
					case HCI_CMND_NVMEM_WRITE_PATCH:
					case HCI_NETAPP_PING_REPORT:
					case HCI_EVNT_MDNS_ADVERTISE:
						// return CC3K_TIMEOUT_ERR for errored out on timeout
						*(signed char *)pRetParams = CC3K_TIMEOUT_ERR;
						break;
						
					case HCI_CMND_SETSOCKOPT:
					case HCI_CMND_WLAN_CONNECT:
					case HCI_CMND_WLAN_IOCTL_STATUSGET:
					case HCI_EVNT_WLAN_IOCTL_ADD_PROFILE:
					case HCI_CMND_WLAN_IOCTL_DEL_PROFILE:
					case HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY:
					case HCI_CMND_WLAN_IOCTL_SET_SCANPARAM:
					case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START:
					case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP:
					case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX:
					case HCI_CMND_EVENT_MASK:
					case HCI_EVNT_WLAN_DISCONNECT:
					case HCI_EVNT_SOCKET:
					case HCI_EVNT_BIND:
					case HCI_CMND_LISTEN:
					case HCI_EVNT_CLOSE_SOCKET:
					case HCI_EVNT_CONNECT:
					case HCI_EVNT_NVMEM_WRITE:
						// return CC3K_TIMEOUT_ERR for errored out on timeout
						*(long *)pRetParams = CC3K_TIMEOUT_ERR;
						break;
						
					case HCI_EVNT_BSD_GETHOSTBYNAME:
						// expects a tBsdGethostbynameParams, set error on retVal and default outputAddress
						((tBsdGethostbynameParams *)pRetParams)->retVal = CC3K_TIMEOUT_ERR;
						((tBsdGethostbynameParams *)pRetParams)->outputAddress = 0;				
						break;
					
					case HCI_EVNT_READ_SP_VERSION:
					case HCI_CMND_SIMPLE_LINK_START:
					case HCI_EVNT_ACCEPT:
					case HCI_EVNT_RECV:
					case HCI_EVNT_RECVFROM:
						{
							// these don't do anything, we're not in a blocking loop for this
							break;
						}

					case HCI_EVNT_SEND:
					case HCI_EVNT_SENDTO:
						// expects a tBsdReadReturnParams, set the iNumberOfBytes to zero
						((tBsdReadReturnParams *)pRetParams)->iNumberOfBytes = 0;
						break;
						
					case HCI_EVNT_SELECT:
						// expects a tBsdSelectRecvParams, set the isStatus
						((tBsdSelectRecvParams *)pRetParams)->iStatus = CC3K_TIMEOUT_ERR;
						break;
						
					case HCI_CMND_GETSOCKOPT:
						// HCI_CMND_GETSOCKOPT expects a tBsdGetSockOptReturnParams, set the error code
						((tBsdGetSockOptReturnParams *)pRetParams)->iStatus = CC3K_TIMEOUT_ERR;
						break;
						
					case HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS:
						// no results on error
						*(unsigned char *)pRetParams = 0;
						break;
						
					case HCI_NETAPP_IPCONFIG:
						// set all values to zero
						memset(pRetParams,0,sizeof(tNetappIpconfigRetArgs));
						break;
				}
				
				TM_DEBUG("HCI Event error on command: %x", tSLInformation.usRxEventOpcode);

				// past maximum wait, get out of here
				return NULL;
			}

		}
#endif

	}

}
예제 #5
0
파일: lua_tm.c 프로젝트: lizhizhou/runtime
static int l_tm_uptime_micro (lua_State* L)
{
  lua_pushnumber(L, tm_uptime_micro());
  return 1;
}