simplekeys.c 15.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
/*
	Copyright by Optek in 2015
*/

#include "c_def.h"
#include "debug.h"
#include "include/bt_def.h"
#include "include/bluetooth.h"
#include "include/hci.h"
#include "include/hci_core.h"
#include "include/l2cap.h"
#include "../blueZ/uuid.h"
//#include "../blueZ/bluetooth.h"
//#include "../blueZ/sdp_lib.h"

#include "include/list.h"
#include "malloc.h"
#include "include/g_slist.h"

#include "../attrib/att.h"
#include "../attrib/gattrib.h"
#include "../attrib/gatt.h"
#include "../attrib/gatt_profile_uuid.h"

#include "simplekeys.h"

#include "../attrib/gatt-service.h"

/*********************************************************************
 * Profile Attributes - variables
 */

// SK Service attribute
static const bt_uuid_t skService = { ATT_BT_UUID_SIZE, SK_SERV_UUID };

static const bt_uuid_t skPressedUuid = { ATT_BT_UUID_SIZE, SK_KEYPRESSED_UUID };
static const bt_uuid_t skReleasedUuid = { ATT_BT_UUID_SIZE, SK_KEYRELEASED_UUID };
 

// Key Pressed State Characteristic
static uint8_t skKeyPressed = 0;
static uint8_t skKeyReleased = 0;

static uint8_t skNotificationEnable = 0;

// Key Pressed Characteristic Configs
static gattCharCfg_t skConfig[GATT_MAX_NUM_CONN];

// Key Pressed Characteristic User Description
static const uint8_t skCharUserDesp[16] = "Key State\0";

#define BLE_NOTIFY_DEBUG

/*********************************************************************
 * Profile Attributes - Table
 */

//static gattAttribute_t simplekeysAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] = 
static gattAttribute_t simplekeysAttrTbl[] = 
{
  // Simple Keys Service
  { 
    //{ ATT_BT_UUID_SIZE, GATT_PRIM_SVC_UUID }, /* type */
	  &gattPrimSvc,
    GATT_PROP_READ,                         /* permissions */
    0,                                        /* handle */
    (uint8_t *)&skService                       /* pValue */
  },

    // Characteristic Declaration for Keys
    { 
      //{ ATT_BT_UUID_SIZE, GATT_CHARAC_UUID },
		  &gattCharac,
      GATT_PROP_READ, 
      0,
#ifdef BLE_NOTIFY_DEBUG
      &gattNotifyProps							//key notify 
#else
	  &gattWriteNoRespProps					//Key write
#endif

    },

      // Characteristic Value- Key Pressed
      { 
        //{ ATT_BT_UUID_SIZE, SK_KEYPRESSED_UUID },
			  &skPressedUuid,
        0, 
        0, 
        &skKeyPressed 
      },


#ifndef BLE_NOTIFY_DEBUG
		{ 
				  //{ ATT_BT_UUID_SIZE, GATT_CHARAC_UUID },
				  &gattCharac,
					  GATT_PROP_READ, 
					  0,
					  &gattWriteNoRespProps 
		},
			  
		// Characteristic Value- Key Pressed
		{ 
					  //{ ATT_BT_UUID_SIZE, SK_KEYPRESSED_UUID },
					  &skReleasedUuid,
						  0, 
						  0, 
						  &skKeyReleased 
		},
#endif	//BLE_NOTIFY_DEBUG

      // Characteristic configuration
      { 
        //{ ATT_BT_UUID_SIZE, GATT_CLIENT_CHARAC_CFG_UUID },
			&gattClientCharacCFG,
		GATT_PROP_READ | GATT_PROP_WRITE,
        0, 
        (uint8_t *)skConfig 
      },

#if 0
      // Characteristic User Description
      { 
        //{ ATT_BT_UUID_SIZE, GATT_CHARAC_USER_DESC_UUID },
		&gattCharacUserDesc,
        GATT_PROP_READ, 
        0, 
        skCharUserDesp 
      },
#endif
      
};



/*********************************************************************
 * LOCAL FUNCTIONS
 */
static uint8_t simplekeysReadAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
                             uint8_t *pValue, uint8_t *pLen, uint16_t offset, uint8_t maxLen );
static bStatus_t simplekeysWriteAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
                                  uint8_t *pValue, uint8_t len, uint16_t offset );
//static void battNotifyCB( linkDBItem_t *pLinkItem );
//static uint8_t battMeasure( void );
//static void battNotifyLevel( void );

/*********************************************************************
 * PROFILE CALLBACKS
 */
// Battery Service Callbacks
const gattServiceCBs_t simplekeysCBs =
{
  NULL,
  NULL,
  simplekeysReadAttrCB,  // Read callback function pointer
  simplekeysWriteAttrCB, // Write callback function pointer
  NULL             // Authorization callback function pointer
};

/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      Batt_AddService
 *
 * @brief   Initializes the Battery Service by registering
 *          GATT attributes with the GATT server.
 *
 * @return  Success or Failure
 */
bStatus_t simplekeys_AddService( void )
{
  uint8_t status;

  skNotificationEnable = 0;

  // Initialize Client Characteristic Configuration attributes
  GATTServApp_InitCharCfg( INVALID_CONNHANDLE, skConfig );

  // Register GATT attribute list and CBs with GATT Server App
  status = GATTServApp_RegisterService( simplekeysAttrTbl,
                                        GATT_NUM_ATTRS( simplekeysAttrTbl ),
                                        &simplekeysCBs );

  return ( status );
}


static uint8_t simplekeysReadAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
							  uint8_t *pValue, uint8_t *pLen, uint16_t offset, uint8_t maxLen )
{
	bStatus_t status = SUCCESS;
	uint16_t uuid = pAttr->pType->value.u16_val; 
	
	// Make sure it's not a blob operation (no attributes in the profile are long)
	if ( offset > 0 )
	{
		*pLen = 0;
		return ( ATT_ERR_ATTR_NOT_LONG );
	}
	

	*pLen = 0;
	status = ATT_ERR_ATTR_NOT_FOUND;

	
  return ( status );
}

void app_cmd_ble_key_pressed(u8 key);
void app_cmd_ble_key_released(u8 key);
void ukParmSend (FUNCTION key_func, U32 parm1);

static bStatus_t simplekeysWriteAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
                                  uint8_t *pValue, uint8_t len, uint16_t offset )
{
	bStatus_t status = SUCCESS;
	uint16_t uuid = pAttr->pType->value.u16_val; 
	uint8_t key;
	
	
	// Make sure it's not a blob operation (no attributes in the profile are long)
	if ( offset > 0 )
	{
		return ( ATT_ERR_ATTR_NOT_LONG );
	}
	
	switch ( uuid )
	{ 
	case SK_KEYPRESSED_UUID:
		key = pValue[0];		
		ukParmSend(app_cmd_ble_key_pressed,(u32) key);
		break;
	case SK_KEYRELEASED_UUID:
		key = pValue[0];
		ukParmSend(app_cmd_ble_key_released,(u32) key);		
		break;
		
	case GATT_CLIENT_CHARAC_CFG_UUID:
		//0x0001	notified
		//0x0002	indicated
		skNotificationEnable = pValue[0] & 0x01;

		//DBG_Assert(FALSE);
		break;
		
	default:
		// Should never get here!
		status = ATT_ERR_ATTR_NOT_FOUND;
		break;
	}
	
	
	return ( status );
}

struct _GAttrib *find_att_sess_only_by_index(int index);
size_t att_get_session_mtu (GAttrib *session);

void att_send_data(void *pIO, struct sk_buff *sk_att);
void att_key_notification_send (u32 parm1, u32 parm2, u32 parm3);

const U8 STRING_OPEN1[] = {"CD    Open\0\0"};
void att_key_notification_send1(void)
{
	att_key_notification_send (STRING_OPEN1,8,NULL);

}
void att_key_notification_send (u32 parm1, u32 parm2, u32 parm3)
{
	GAttrib *session;
	struct sk_buff *gatt_sk_buf;
	u16 mtu;
	u16 handle;
	
	
	session = find_att_sess_only_by_index(0);
	if (session == NULL) {
		DBG_Printf ("No gatt service\n\r");
		return;	
	}

	if (skNotificationEnable == 0x0) {
		DBG_Printf ("Notify is not configed\n\r");
		return;	
	}

	//handle = simplekeysAttrTbl[0].handle;
	handle = simplekeysAttrTbl[2].handle;
	
	if (handle == 0) {
		DBG_Assert (FALSE);
		DBG_Printf ("No disp service\n\r");
		return;	
	}

	mtu = att_get_session_mtu(session);
	gatt_sk_buf = bt_skb_alloc((MAX_BT_SEND_SKB-BT_SKB_RESERVE), GFP_ATOMIC);
	
	gatt_sk_buf->len = enc_notification(handle, (uint8_t *) parm1, parm2, (uint8_t *) gatt_sk_buf->data, mtu);
	
	if (gatt_sk_buf->len > mtu) {
		DBG_Printf ("Notify message is too long %d mtu %d\n\r", gatt_sk_buf->len,mtu);
		//cut some
		gatt_sk_buf->len = mtu;
	}
	
	att_send_data(session, gatt_sk_buf);
}


/*********************************************************************
 * Profile Attributes - variables
 */

// RTU Service attribute
static const bt_uuid_t rtuService = { ATT_BT_UUID_SIZE, APP_SERV_UUID };

// RTU characteristic
static const bt_uuid_t phone_send_rtuTime_Uuid = { ATT_BT_UUID_SIZE, APP_SERV_W_UUID };
//static const bt_uuid_t rtuTime_send_phone_Uuid = { ATT_BT_UUID_SIZE, APP_SERV_W_UUID/*APP_SERV_R_UUID */};
static const bt_uuid_t rtuTime_send_phone_Uuid = { ATT_BT_UUID_SIZE, APP_SERV_R_UUID };
static const bt_uuid_t rtuTime_notify_Uuid = { ATT_BT_UUID_SIZE, APP_SERV_N_UUID };


// RTU State Characteristic
static uint8_t rtuTime[32];
static uint8_t rtu_Update_State_Time[32];
static uint8_t rtu_notify_State_Time[32];

static uint8_t rtuNotificationEnable = 0;

// RTU Characteristic Configs
static gattCharCfg_t rtuConfig[GATT_MAX_NUM_CONN];

// Key Pressed Characteristic User Description
static const uint8_t LDRCCharUserDesc[22] = "Linking Digital RC\0";

void att_key_notification_send_ldrc (u32 parm1, u32 parm2, u32 parm3);

/*********************************************************************
 * Profile Attributes - Table
 */

static gattAttribute_t rtuAttrTbl[] = 
{
	  //Service
	  { 
	    //{ ATT_BT_UUID_SIZE, GATT_PRIM_SVC_UUID }, /* type */
		&gattPrimSvc,
	    GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_NOTIFY | GATT_PROP_WRITE_NO_RSP,                         /* permissions */
	    0,                                        /* handle */
	    (uint8_t *)&rtuService                       /* pValue */
	  },
      // Characteristic Declaration
      { 
        //{ ATT_BT_UUID_SIZE, GATT_CHARAC_UUID },
  	  	&gattCharac,
  	  	GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP,
        0,
        //&gattReadProps
        &gattWriteNoRespProps
      },
  
      // Characteristic Time Update Control Point
      { 
		&phone_send_rtuTime_Uuid,
		GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP,
        0, 
        rtuTime 
      },

      // Characteristic Declaration
      { 
        //{ ATT_BT_UUID_SIZE, GATT_CHARAC_UUID },
  	  	&gattCharac,
  	  	GATT_PROP_READ, 
        0,
        &gattReadProps
      },
  
      // Characteristic Time Update Control Point
      { 
	    &rtuTime_send_phone_Uuid,
		GATT_PROP_READ, 
        0, 
        rtu_Update_State_Time 
      },
        
      { 
  	  	&gattCharac,
  	  	GATT_PROP_NOTIFY, 
        0,
        &gattNotifyProps
      },
  
      { 
	    &rtuTime_notify_Uuid,
		GATT_PROP_NOTIFY, 
        0, 
        rtu_notify_State_Time 
      },

/*
	// Characteristic configuration
	{ 
		//{ ATT_BT_UUID_SIZE, GATT_CLIENT_CHARAC_CFG_UUID },
		&gattClientCharacCFG,
		GATT_PROP_READ | GATT_PROP_WRITE,
		0, 
		(uint8_t *)rtuConfig 
	},

	// Characteristic User Description
	{ 
		//{ ATT_BT_UUID_SIZE, GATT_CHARAC_USER_DESC_UUID },
		&gattCharacUserDesc,
		GATT_PROP_READ, 
		0, 
		LDRCCharUserDesc 
	},
*/
};

/*********************************************************************
 * LOCAL FUNCTIONS
 */
static uint8_t RTUReadAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
                             uint8_t *pValue, uint8_t *pLen, uint16_t offset, uint8_t maxLen );
static bStatus_t RTUWriteAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
                                  uint8_t *pValue, uint8_t len, uint16_t offset );
//static void battNotifyCB( linkDBItem_t *pLinkItem );
//static uint8_t battMeasure( void );
//static void battNotifyLevel( void );

/*********************************************************************
 * PROFILE CALLBACKS
 */
// RTU Callbacks
const gattServiceCBs_t RTUCBs =
{
  NULL,
  NULL,
  RTUReadAttrCB,  // Read callback function pointer
  RTUWriteAttrCB, // Write callback function pointer
  NULL             // Authorization callback function pointer
};

/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      Batt_AddService
 *
 * @brief   Initializes the Battery Service by registering
 *          GATT attributes with the GATT server.
 *
 * @return  Success or Failure
 */
bStatus_t reference_time_update_AddService( void )
{
  uint8_t status;

  rtuNotificationEnable = 0;
/*
  // Initialize Client Characteristic Configuration attributes
  GATTServApp_InitCharCfg( INVALID_CONNHANDLE, rtuConfig );
*/
  // Register GATT attribute list and CBs with GATT Server App
  status = GATTServApp_RegisterService( rtuAttrTbl,
                                        GATT_NUM_ATTRS( rtuAttrTbl ),
                                        &RTUCBs );

  return ( status );
}

U8 bleread_buf[16];
static uint8_t RTUReadAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
							  uint8_t *pValue, uint8_t *pLen, uint16_t offset, uint8_t maxLen )
{
	bStatus_t status = SUCCESS;
	uint16_t uuid = pAttr->pType->value.u16_val; 
	uint16_t i;
	
	DBG_Printf("APP ruuid:0x%x\n", uuid);
	
	// Make sure it's not a blob operation (no attributes in the profile are long)
	if ( offset > 0 )
	{
		return ( ATT_ERR_ATTR_NOT_LONG );
	}

	// Measure battery level if reading level
	if ( uuid == 0xF002 )
	{
		DBG_Printf("ble tx data:");
		for(i=0; i<16; i++)
		{
			DBG_Printf("0x%x, ", bleread_buf[i]);
		}
		DBG_Printf("\n");	
		
		*pLen = 16;
		for (i=0; i<16; i++)
		{
		 	pValue[i] = bleread_buf[i];				//80%
		}
	}
/*
	else if ( uuid == GATT_REPORT_REFERENCE )
	{
		*pLen = HID_REPORT_REF_LEN;

		CFasm_memcpy( pValue, pAttr->pValue, HID_REPORT_REF_LEN );
	}
*/
	else
	{
		*pLen = 0;
		status = ATT_ERR_ATTR_NOT_FOUND;
	}

	//att_key_notification_send_ldrc(bleread_buf, 10, NULL);
	
/*
	// Measure read current time value
	if ( uuid == TIME_UPDATE_STATE_UUID )
	{		
		//*pLen = 10;
		//timeBuf = pValue;

	}
	else
	{
		*pLen = 0;
		status = ATT_ERR_ATTR_NOT_FOUND;
	}
*/	
  return ( status );

}

void app_ble_update_rxdata(U8 *p);

uint8_t Phoneble_rxBuf[16];

static bStatus_t RTUWriteAttrCB( GAttrib *pConn, gattAttribute_t *pAttr,
                                  uint8_t *pValue, uint8_t len, uint16_t offset )
{
	bStatus_t status = SUCCESS;
	uint16_t uuid = pAttr->pType->value.u16_val; 
	uint8_t i;
	uint8_t buf[16];
	DBG_Printf("APP wuuid:0x%x,%d\n", uuid, len );
	
	memset(Phoneble_rxBuf,0,sizeof(Phoneble_rxBuf));
	// Make sure it's not a blob operation (no attributes in the profile are long)
	if ( offset > 0 )
	{
		return ( ATT_ERR_ATTR_NOT_LONG );
	}
	
	switch ( uuid )
	{ 
		case 0xF001:
			DBG_Printf("ble tx data:\n");
			for(i=0; i<len; i++)
			{
				Phoneble_rxBuf[i] = pValue[i];
				//DBG_Printf("0x%x, ", buf[i]);
			}
			DBG_Printf("\n");	

			ukParmSend(app_ble_update_rxdata, Phoneble_rxBuf);
			
			break;
			
	/*	case GATT_CLIENT_CHARAC_CFG_UUID:
			//0x0001	notified
			//0x0002	indicated
			rtuNotificationEnable = pValue[0] & 0x01;

			//DBG_Assert(FALSE);
			break;*/
			
		default:
			// Should never get here!
			status = ATT_ERR_ATTR_NOT_FOUND;
			//DBG_Printf("BLE write status==false\n");
			break;
	}
	
	//DBG_Printf("BLE write status==TRUE\n");
	return ( status );
}


struct _GAttrib *find_att_sess_only_by_index(int index);
size_t att_get_session_mtu (GAttrib *session);

void att_send_data(void *pIO, struct sk_buff *sk_att);


void att_key_notification_send_ldrc (u32 parm1, u32 parm2, u32 parm3)
{
	GAttrib *session;
	struct sk_buff *gatt_sk_buf;
	u16 mtu;
	u16 handle;
	
	DBG_Printf("BLE notify enter\n\r");


	session = find_att_sess_only_by_index(1);
	if (session == NULL) {
		DBG_Printf ("No nore than 1 service\n\r");
		//return;	
	}

	session = find_att_sess_only_by_index(0);
	if (session == NULL) {
		DBG_Printf ("No gatt service\n\r");
		return;	
	}
		DBG_Printf ("session:0x%x\n\r",session);

/*
	if (skNotificationEnable == 0x0) {
		DBG_Printf ("Notify is not configed\n\r");
		return;	
	}
*/
	//handle = simplekeysAttrTbl[0].handle;
	handle = rtuAttrTbl[6].handle;
	DBG_Printf ("handle:0x%x\n\r",handle);
	if (handle == 0) {
		DBG_Assert (FALSE);
		DBG_Printf ("No disp service\n\r");
		return;	
	}

	mtu = att_get_session_mtu(session);
	
	gatt_sk_buf = bt_skb_alloc((MAX_BT_SEND_SKB-BT_SKB_RESERVE), GFP_ATOMIC);
	
	gatt_sk_buf->len = enc_notification(handle, (uint8_t *) parm1, parm2, (uint8_t *) gatt_sk_buf->data, mtu);
	
	if (gatt_sk_buf->len > mtu) {
		DBG_Printf ("Notify message is too long %d mtu %d\n\r", gatt_sk_buf->len,mtu);
		//cut some
		gatt_sk_buf->len = mtu;
	}
	
	att_send_data(session, gatt_sk_buf);
}