Esempio n. 1
0
 /**
  * Get pin number of Gpio. If raw param is True will return the
  * number as used within sysfs. Invalid will return -1.
  *
  * @param raw (optional) get the raw gpio number.
  * @return Pin number
  */
 int
 getPin(bool raw = false)
 {
     if (raw) {
         return mraa_gpio_get_pin_raw(m_gpio);
     }
     return mraa_gpio_get_pin(m_gpio);
 }
Esempio n. 2
0
int main(int argc, char* argv[]) {
    mraa_gpio_context gpio;

    if (argc != 2) {
        printf("gpio [pin]\n");
        exit(1);
    }

    int pin = atoi(argv[1]);

    gpio = mraa_gpio_init(pin);

    if (gpio == NULL) {
        return 1;
    }

    // mraa_gpio_period_ms(4); // communication process is about 4ms(Data sheet)
    // Send start signal

    int readData = 0;
    int count = 0;

    printf("pin number %d\n", mraa_gpio_get_pin(gpio));
    
    while (1) {
        get_data(gpio, &readData);

        count++;

        if (count == 50) {
            printf("aaaa\n");
            break;
        }

        usleep(1000000);
    }

    mraa_gpio_close(gpio);

    printf("GPIO closed.\n");


    return 0;
}
Esempio n. 3
0
File: blue.c Progetto: aelane/TeddyB
int main(void) {
	int i, j, err, sock, dev_id = -1;
	struct hci_dev_info dev_info;
	inquiry_info *info = NULL;
	bdaddr_t target;
	char addr[19] = { 0 };
	char name[248] = { 0 };
	uuid_t uuid = { 0 };
	//Change this to your apps UUID
	char *uuid_str="4e5d48e0-75df-11e3-981f-0800200c9a66";
	uint32_t range = 0x0000ffff;
	sdp_list_t *response_list = NULL, *search_list, *attrid_list;
	int s, loco_channel = -1, status;
	struct sockaddr_rc loc_addr = { 0 };
	pthread_t blueThread;
	
	FILE *fp_Setting;
	char message_Buffer[64];
	char mode[30], language[16], topic[16], topicLen[16];
	size_t len = 16;
	(void) signal(SIGINT, SIG_DFL);
	
	changeTopic_flag = 1;
///////////////////////////MRAA/////////////////////////////	
	//Initialize MRAA
        mraa_init();
        
        //Initialize MRAA Pin 8 == IO8 == GP49
        mraa_gpio_context BearButton = mraa_gpio_init(8);

        //Check for successful initialization or else return
        if (BearButton == NULL){
                printf("Error initializing Push to Talk Pin, IO2\n");
                return -1;
        }
        
		//Set pin to an input
		mraa_gpio_dir(BearButton, MRAA_GPIO_IN);
		printf("Set Pin to an input\n");
 
        
        int curr_pin = mraa_gpio_get_pin(BearButton);
        printf("The current pin number is %d\n", curr_pin);
        int curr_raw = mraa_gpio_get_pin_raw(BearButton);
        printf("The raw pin number is %d\n", curr_raw);

        printf("Going to start reading the button via polling\n");
	
//////////////////////AWS///////////////////////////////////
	aws_flag = 0;	
	IoT_Error_t rc = NONE_ERROR;
	char HostAddress[255] = AWS_IOT_MQTT_HOST;
	char certDirectory[PATH_MAX + 1] = "/AWS/SDK/certs/";
	uint32_t port = AWS_IOT_MQTT_PORT;
	
	MQTTMessageParams Msg = MQTTMessageParamsDefault;
	Msg.qos = QOS_0;
	char cPayload[100];
	Msg.pPayload = (void *) cPayload;
	
	char rootCA[PATH_MAX + 1];
	char clientCRT[PATH_MAX + 1];
	char clientKey[PATH_MAX + 1];
	
	char cafileName[] = AWS_IOT_ROOT_CA_FILENAME;
	char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME;
	char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME;
	
	sprintf(rootCA, "/%s/%s", certDirectory, cafileName);
	sprintf(clientCRT, "/%s/%s", certDirectory, clientCRTName);
	sprintf(clientKey, "/%s/%s", certDirectory, clientKeyName);
	
	MQTTConnectParams connectParams = MQTTConnectParamsDefault;

	connectParams.KeepAliveInterval_sec = 14000;
	connectParams.isCleansession = true;
	connectParams.MQTTVersion = MQTT_3_1_1;
	connectParams.pClientID = "TED";
	connectParams.pHostURL = HostAddress;
	connectParams.port = port;
	connectParams.isWillMsgPresent = false;
	connectParams.pRootCALocation = rootCA;
	connectParams.pDeviceCertLocation = clientCRT;
	connectParams.pDevicePrivateKeyLocation = clientKey;
	connectParams.mqttCommandTimeout_ms = 2000;
	connectParams.tlsHandshakeTimeout_ms = 5000;
	connectParams.isSSLHostnameVerify = true;// ensure this is set to true for production
	connectParams.disconnectHandler = disconnectCallbackHandler;
	
	MQTTPublishParams CurriculumParams = MQTTPublishParamsDefault;
	CurriculumParams.pTopic = "Bear/Curriculum/Request";
	
	
	MQTTPublishParams MetricsParams = MQTTPublishParamsDefault;
	MetricsParams.pTopic = "Bear/Curriculum/Metrics";

///////////////////////////////////////////////////////////////////////////////////////
	dev_id = hci_get_route(NULL);
	if (dev_id < 0) {
		perror("No Bluetooth Adapter Available");
		exit(1);
	}

	if (hci_devinfo(dev_id, &dev_info) < 0) {
		perror("Can't get device info");
		exit(1);
	}



	sock = hci_open_dev( dev_id );
	if (sock < 0) {
		perror("HCI device open failed");
		free(info);
		exit(1);
	}

	
	if( !str2uuid( uuid_str, &uuid ) ) {
		perror("Invalid UUID");
		free(info);
		exit(1);
	}

	do {
		printf("Scanning ...\n");
		
			sdp_session_t *session;
			int retries;
			int foundit, responses;
			str2ba(TABLET_ADDRESS,&target); 
			memset(name, 0, sizeof(name));
			if (hci_read_remote_name(sock, &target, sizeof(name), name, 0) < 0){
				strcpy(name, "[unknown]");
			}
			
			printf("Found %s  %s, searching for the the desired service on it now\n", addr, name);
			// connect to the SDP server running on the remote machine
sdpconnect:
			session = 0; retries = 0;
			while(!session) {
				session = sdp_connect( BDADDR_ANY, &target, SDP_RETRY_IF_BUSY );
				if(session) break;
				if(errno == EALREADY && retries < 5) {
					perror("Retrying");
					retries++;
					sleep(1);
					continue;
				}
				break;
			}
			if ( session == NULL ) {
				perror("Can't open session with the device");
				continue;
			}
			search_list = sdp_list_append( 0, &uuid );
			attrid_list = sdp_list_append( 0, &range );
			err = 0;
			err = sdp_service_search_attr_req( session, search_list, SDP_ATTR_REQ_RANGE, attrid_list, &response_list);
			sdp_list_t *r = response_list;
			sdp_record_t *rec;
			// go through each of the service records
			foundit = 0;
			responses = 0;
			for (; r; r = r->next ) {
				responses++;
				rec = (sdp_record_t*) r->data;
				sdp_list_t *proto_list;
					
				// get a list of the protocol sequences
				if( sdp_get_access_protos( rec, &proto_list ) == 0 ) {
				sdp_list_t *p = proto_list;

					// go through each protocol sequence
					for( ; p ; p = p->next ) {
						sdp_list_t *pds = (sdp_list_t*)p->data;

						// go through each protocol list of the protocol sequence
						for( ; pds ; pds = pds->next ) {

							// check the protocol attributes
							sdp_data_t *d = (sdp_data_t*)pds->data;
							int proto = 0;
							for( ; d; d = d->next ) {
								switch( d->dtd ) { 
									case SDP_UUID16:
									case SDP_UUID32:
									case SDP_UUID128:
											proto = sdp_uuid_to_proto( &d->val.uuid );
											break;
									case SDP_UINT8:
										if( proto == RFCOMM_UUID ) {
												printf("rfcomm channel: %d\n",d->val.int8);
												loco_channel = d->val.int8;
												foundit = 1;
										}
										break;
								}
							}
						}
						sdp_list_free( (sdp_list_t*)p->data, 0 );
					}
					sdp_list_free( proto_list, 0 );

				}
				if (loco_channel > 0)
					break;

			}
			printf("No of Responses %d\n", responses);
			if ( loco_channel > 0 && foundit == 1 ) {
				printf("Found service on this device, now gonna blast it with dummy data\n");
				s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
				loc_addr.rc_family = AF_BLUETOOTH;
				loc_addr.rc_channel = loco_channel;
				loc_addr.rc_bdaddr = *(&target);
				status = connect(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
				if( status < 0 ) {
					perror("uh oh");
				}

				
				rc = pthread_create(&blueThread, NULL, threadListen, (void *)s);
					if (rc){
						printf("ERROR: %d\n", rc);
						exit(1);
					}
				MQTTSubscribeParams subParams = MQTTSubscribeParamsDefault;
				//Loop through trying to subscribe to AWS incase the first attempt fails, so will keep trying until the app sends the edison proper network info.
				do{
					INFO("Connecting...");
					rc = aws_iot_mqtt_connect(&connectParams);
					
					if (NONE_ERROR != rc) {
						ERROR("Error(%d) connecting to %s:%d", rc, connectParams.pHostURL, connectParams.port);
					}
					
					subParams.mHandler = MQTTcallbackHandler;
					subParams.pTopic = "Bear/Curriculum/Response";
					subParams.qos = QOS_0;

					if (NONE_ERROR == rc) {
						INFO("Subscribing...");
						rc = aws_iot_mqtt_subscribe(&subParams);
						if (NONE_ERROR != rc) {
							ERROR("Error subscribing");
						}
					}
					sleep(1);
				}while(NONE_ERROR != rc);
				aws_flag = 1;
//////////////////////////Start of teaching stuff//////////////////////////////////////////
				do {
					
				
					if(changeTopic_flag){
					
						sprintf(cPayload, "{\"BearID\":\"%s\"}",BEARID);
						Msg.PayloadLen = strlen(cPayload) + 1;
						CurriculumParams.MessageParams = Msg;
						rc = aws_iot_mqtt_publish(&CurriculumParams);
						while(rc == NONE_ERROR && changeTopic_flag){
							rc = aws_iot_mqtt_yield(100);
							INFO("-->sleep");
							sleep(1);
						}								
						changeTopic_flag = 0;
					}
					
					
					fp_Setting = fopen("/Curriculum/BearSettings.txt", "r");
					fgets(language, sizeof(language), fp_Setting);
					fgets(mode, sizeof(mode), fp_Setting);
					fgets(topic, sizeof(topic), fp_Setting);
					fgets(topicLen, sizeof(topicLen), fp_Setting);
					language[strlen(language)-1] = 0;
					mode[strlen(mode)-1] = 0;
					topic[strlen(topic)-1] = 0;
					topicLen[strlen(topicLen)-1] = 0;
					fclose(fp_Setting);

					//Wait for the tablet to tell the edison to start the lesson
					while(strcmp(threadMessage, "learning")){
						printf("Waiting for learning, got: %s\n", threadMessage);
						if(!strcmp(threadMessage, "crash")){
							memset(threadMessage, 0, sizeof(threadMessage));
							goto CRASHED;
						}
						memset(threadMessage, 0, sizeof(threadMessage));
						sleep(1);
					}

					
					printf("Current Mode: %s\n", mode);
					printf("Current Language: %s\n", language);
					printf("Current topic: %s\n", topic);

					//Call a different function depending on what teaching mode it is, also write to the tablet based on the topic info and lesson
					if(!strcmp(mode, "Repeat After Me")){
						sprintf(message_Buffer,"%s,1,%s",language, topicLen);
						status = write(s,message_Buffer,sizeof(message_Buffer));
						if(!strcmp(language, "English")){
							status = repeat_after_me_english(s, topic, MetricsParams, BearButton);
						}
						else{
							status = repeat_after_me_foreign(s, language, topic, MetricsParams, BearButton);
						}
					}
					else if(!strcmp(mode, "English to Foreign")){
						sprintf(message_Buffer,"%s,3,%s",language, topicLen);
						status = write(s,message_Buffer,sizeof(message_Buffer));
						status = english_to_foreign(s, language, topic, MetricsParams, BearButton);
					}
					else if(!strcmp(mode, "Foreign to English")){
						sprintf(message_Buffer,"%s,2,%s",language, topicLen);
						status = write(s,message_Buffer,sizeof(message_Buffer));
						status = foreign_to_english(s, language, topic, MetricsParams, BearButton);
					}
					
					
					changeTopic_flag = 1;
					printf("Changing Topic");
					sleep(5);
				} while (status > 0);
CRASHED:
				printf("\nBluetooth Disconnected\n");
				close(s);
				sdp_record_free( rec );
			}

			sdp_close(session);
			if (loco_channel > 0) {
				goto sdpconnect;
				//break;
			}
		sleep(1);
	} while (1);

	printf("Exiting...\n");
}