Skip to content

Instantly share code, notes, and snippets.

@boring-km
Last active March 18, 2019 04:02
Show Gist options
  • Save boring-km/b742ce94ad8a3b81899e104bc60ff413 to your computer and use it in GitHub Desktop.
Save boring-km/b742ce94ad8a3b81899e104bc60ff413 to your computer and use it in GitHub Desktop.
raspberry_auto_connect.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <bluetooth/rfcomm.h>
bdaddr_t bdaddr_any = {0, 0, 0, 0, 0, 0};
bdaddr_t bdaddr_local = {0, 0, 0, 0xff, 0xff, 0xff};
using namespace std;
// http://stackoverflow.com/questions/309491/how-do-i-read-the-results-of-a-system-call-in-c
int system(const char *string);
int _str2uuid( const char *uuid_str, uuid_t *uuid ) {
/* This is from the pybluez stack */
uint32_t uuid_int[4];
char *endptr;
if( strlen( uuid_str ) == 36 ) {
char buf[9] = { 0 };
if( uuid_str[8] != '-' && uuid_str[13] != '-' &&
uuid_str[18] != '-' && uuid_str[23] != '-' ) {
return 0;
}
// first 8-bytes
strncpy(buf, uuid_str, 8);
uuid_int[0] = htonl( strtoul( buf, &endptr, 16 ) );
if( endptr != buf + 8 ) return 0;
// second 8-bytes
strncpy(buf, uuid_str+9, 4);
strncpy(buf+4, uuid_str+14, 4);
uuid_int[1] = htonl( strtoul( buf, &endptr, 16 ) );
if( endptr != buf + 8 ) return 0;
// third 8-bytes
strncpy(buf, uuid_str+19, 4);
strncpy(buf+4, uuid_str+24, 4);
uuid_int[2] = htonl( strtoul( buf, &endptr, 16 ) );
if( endptr != buf + 8 ) return 0;
// fourth 8-bytes
strncpy(buf, uuid_str+28, 8);
uuid_int[3] = htonl( strtoul( buf, &endptr, 16 ) );
if( endptr != buf + 8 ) return 0;
if( uuid != NULL ) sdp_uuid128_create( uuid, uuid_int );
} else if ( strlen( uuid_str ) == 8 ) {
// 32-bit reserved UUID
uint32_t i = strtoul( uuid_str, &endptr, 16 );
if( endptr != uuid_str + 8 ) return 0;
if( uuid != NULL ) sdp_uuid32_create( uuid, i );
} else if( strlen( uuid_str ) == 4 ) {
// 16-bit reserved UUID
int i = strtol( uuid_str, &endptr, 16 );
if( endptr != uuid_str + 4 ) return 0;
if( uuid != NULL ) sdp_uuid16_create( uuid, i );
} else {
return 0;
}
return 1;
}
sdp_session_t *register_service(uint8_t rfcomm_channel) {
const char *service_name = "Armatus Bluetooth server";
const char *svc_dsc = "A HERMIT server that interfaces with the Armatus Android app";
const char *service_prov = "Armatus";
uuid_t root_uuid, l2cap_uuid, rfcomm_uuid, svc_uuid,
svc_class_uuid;
sdp_list_t *l2cap_list = 0,
*rfcomm_list = 0,
*root_list = 0,
*proto_list = 0,
*access_proto_list = 0,
*svc_class_list = 0,
*profile_list = 0;
sdp_data_t *channel = 0;
sdp_profile_desc_t profile;
sdp_record_t record = { 0 };
sdp_session_t *session = 0;
// set the general service ID
//sdp_uuid128_create(&svc_uuid, &svc_uuid_int);
_str2uuid("00001101-0000-1000-8000-00805F9B34FB",&svc_uuid);
sdp_set_service_id(&record, svc_uuid);
char str[256] = "";
sdp_uuid2strn(&svc_uuid, str, 256);
printf("Registering UUID %s\n", str);
// set the service class
sdp_uuid16_create(&svc_class_uuid, SERIAL_PORT_SVCLASS_ID);
svc_class_list = sdp_list_append(0, &svc_class_uuid);
sdp_set_service_classes(&record, svc_class_list);
// set the Bluetooth profile information
sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
profile.version = 0x0100;
profile_list = sdp_list_append(0, &profile);
sdp_set_profile_descs(&record, profile_list);
// make the service record publicly browsable
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
root_list = sdp_list_append(0, &root_uuid);
sdp_set_browse_groups(&record, root_list);
// set l2cap information
sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
l2cap_list = sdp_list_append(0, &l2cap_uuid);
proto_list = sdp_list_append(0, l2cap_list);
// register the RFCOMM channel for RFCOMM sockets
sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
channel = sdp_data_alloc(SDP_UINT8, &rfcomm_channel);
rfcomm_list = sdp_list_append(0, &rfcomm_uuid);
sdp_list_append(rfcomm_list, channel);
sdp_list_append(proto_list, rfcomm_list);
access_proto_list = sdp_list_append(0, proto_list);
sdp_set_access_protos(&record, access_proto_list);
// set the name, provider, and description
sdp_set_info_attr(&record, service_name, service_prov, svc_dsc);
// connect to the local SDP server, register the service record,
// and disconnect
session = sdp_connect(&bdaddr_any, &bdaddr_local, SDP_RETRY_IF_BUSY);
sdp_record_register(session, &record, 0);
// cleanup
sdp_data_free(channel);
sdp_list_free(l2cap_list, 0);
sdp_list_free(rfcomm_list, 0);
sdp_list_free(root_list, 0);
sdp_list_free(access_proto_list, 0);
sdp_list_free(svc_class_list, 0);
sdp_list_free(profile_list, 0);
return session;
}
void write_server(int client) {
// send data to the client
int bytes_sent;
char ttt[1024] = { 0 };
sprintf(ttt, "sudo iwlist wlan0 scan | grep ESSID");
printf("%s\n",ttt);
FILE* streamt = popen(ttt, "r");
ostringstream outputt;
while( !feof(streamt) && !ferror(streamt) ) {
char bufs[1024];
int bytesReads = fread( bufs, 1, 1024, streamt );
outputt.write( bufs, bytesReads );
}
fclose(streamt);
string resultt = outputt.str();
char bufft[4096] = { 0 };
char message[4096] = { 0 };
strcpy(message, resultt.c_str());
printf("%s\n", message);
bytes_sent = write(client, message, strlen(message));
if (bytes_sent > 0) {
printf("message = %s\nsize = %d\n", message, strlen(message));
}
}
int init_server() {
int port = 3, result, sock, client, bytes_read, bytes_sent;
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buffer[1024] = { 0 };
socklen_t opt = sizeof(rem_addr);
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = bdaddr_any;
loc_addr.rc_channel = (uint8_t) port;
// register service
sdp_session_t *session = register_service(port);
// allocate socket
sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
printf("socket() returned %d\n", sock);
// bind socket to port 3 of the first available
result = bind(sock, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
printf("bind() on channel %d returned %d\n", port, result);
// put socket into listening mode
result = listen(sock, 1);
printf("listen() returned %d\n", result);
//sdpRegisterL2cap(port);
// accept one connection
printf("calling accept()\n");
client = accept(sock, (struct sockaddr *)&rem_addr, &opt);
printf("accept() returned %d\n", client);
ba2str(&rem_addr.rc_bdaddr, buffer);
fprintf(stderr, "accepted connection from %s\n", buffer);
memset(buffer, 0, sizeof(buffer));
char first_msg[1024] = { 0 };
sprintf(first_msg, "test");
//write(client, first_msg, strlen(first_msg));
return client;
}
char nt[1024] = { 0 };
char *read_server(int client) {
// read data from the client
int bytes_read;
char input[1024] = { 0 };
bytes_read = read(client, input, sizeof(input));
if (bytes_read > 0) {
printf("received [%s]\n", input);
int size = sizeof(input);
if(input[0] == '1'){
printf("device register...\n");
write_server(client);
input[0] = '\0';
} else if(input[0] == '2'){
int i = 1;
for(i = 1; input[i]; i++) {
input[i-1] = input[i];
}
input[i-1] = '\0';
for(int j = 0; j < size; j++) {
nt[j] = input[j];
}
input[0] = '\0';
} else if(input[0] == '3'){
int i = 0;
for(i = 1; input[i]; i++) {
input[i-1] = input[i];
}
input[i-1] = '\0';
char summ[1024] = { 0 };
char *ptr = strtok(nt, "0");
char *ssid = strtok(nt, "0");
sprintf(summ, "wpa_passphrase %s %s", ptr, input);
printf("%s\n",summ);
FILE* stream = popen(summ, "r");
ostringstream output;
while( !feof(stream) && !ferror(stream) )
{
char buf[1024];
int bytesRead = fread( buf, 1, 1024, stream );
output.write( buf, bytesRead );
}
fclose(stream);
string result = output.str();
cout << result << endl;
char buff[1024] = { 0 };
char res[1024] = { 0 };
strcpy(res, result.c_str());
sprintf(buff, "echo \"%s\" > wifi.txt", res);
system(buff);
FILE *pFile = NULL;
pFile = fopen( "wifi.txt", "r" );
if( pFile != NULL )
{
char real[1024];
char strTemp[1024];
char end[2048];
char final[2048];
char *pStr;
int count = 0;
while( !feof( pFile ) )
{
count++;
pStr = fgets( strTemp, sizeof(strTemp), pFile );
if(count==4) {
strcat(real,strTemp);
}
//printf( "%s", );
}
fclose( pFile );
sprintf(end, "network={\n\tssid=\\\"%s\\\"\n%s}", ssid, real);
sprintf(final, "echo \"%s\" >> /etc/wpa_supplicant/wpa_supplicant.conf", end);
printf("final: %s\n", final);
system(final);
}
else
{
//
}
input[0] = '\0';
printf("wifi change complete!");
} else if(input[0] == '4') {
int i = 0;
for(i = 1; input[i]; i++) {
input[i-1] = input[i];
}
input[i-1] = '\0';
char buff[1024] = { 0 };
sprintf(buff, "echo %s > register.txt", input);
system(buff);
printf("random number saved");
system("echo 2 > first.txt");
input[0] = '\0';
//system("reboot");
} else if(input[0] == '9') {
int i = 0;
for(i = 1; input[i]; i++) {
input[i-1] = input[i];
}
input[i-1] = '\0';
char buff[1024] = { 0 };
char final[1024] = { 0 };
sprintf(buff, "network={\n\tssid=\\\"%s\\\"\n\tkey_mgmt=NONE\n}", input);
sprintf(final, "echo \"%s\" >> /etc/wpa_supplicant/wpa_supplicant.conf", buff);
system(final);
}
return input;
} else {
return NULL;
}
}
int main( int argc, char** argv )
{
char tmp[256];
ifstream fin("first.txt");
fin >> tmp;
system("sudo hciconfig hci0 piscan");
if(tmp[0]=='1') {
//sprintf(buff, "echo \"%s\" > wifilist.txt", res);
//system(buff);
int client = init_server();
while(1) {
char *recv_message = read_server(client);
printf("received:%s\n", recv_message);
//write_server(client, recv_message);
}
} else if(tmp[0]=='2') {
system("sudo python3 /home/pi/Downloads/register.py");
system("echo sudo python3 /home/pi/Downloads/uploads_dust.py > /home/pi/Downloads/run.sh");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment