1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.pmc;
18 
19 import android.app.AlarmManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.Bundle;
25 import android.util.Log;
26 
27 /**
28  * PMC Receiver functions for GATT Client and Server.
29  */
30 public class GattPMCReceiver extends BroadcastReceiver {
31     public static final String TAG = "GATTPMC";
32     public static final String GATTPMC_INTENT = "com.android.pmc.GATT";
33     private final GattClientListener mGattClientListener;
34     private final GattServer mGattServer;
35 
36     /**
37      * Constructor to be called by PMC
38      *
39      * @param context - PMC will provide a context
40      * @param alarmManager - PMC will provide alarmManager
41      */
GattPMCReceiver(Context context, AlarmManager alarmManager)42     public GattPMCReceiver(Context context, AlarmManager alarmManager) {
43         Log.d(TAG, "Start GattPMCReceiver()");
44 
45         // Prepare for setting alarm service
46         mGattClientListener = new GattClientListener(context, alarmManager);
47         mGattServer = new GattServer(context);
48 
49         // RegisterAlarmReceiver for GattListener
50         context.registerReceiver(mGattClientListener,
51                 new IntentFilter(GattClientListener.GATTCLIENT_ALARM),
52                 Context.RECEIVER_EXPORTED_UNAUDITED);
53         Log.d(TAG, "Start GattPMCReceiver()");
54     }
55 
56     /**
57      * Method to receive the broadcast from python client for PMC commands
58      *
59      * @param context - system will provide a context to this function
60      * @param intent - system will provide an intent to this function
61      */
62     @Override
onReceive(Context context, Intent intent)63     public void onReceive(Context context, Intent intent) {
64         Log.d(TAG, "Intent: " + intent.getAction());
65         if (intent.getAction().equals(GATTPMC_INTENT)) {
66             Bundle extras = intent.getExtras();
67             int startTime = 0, writeTime = 0, idleTime = 0, Repetitions = 1;
68             String str;
69 
70             if (extras == null) {
71                 Log.e(TAG, "No parameters specified");
72                 return;
73             }
74 
75             if (extras.containsKey("GattServer")) {
76                 // this is for Gatt Server
77                 Log.d(TAG, "For Gatt Server");
78                 mGattServer.startGattServer();
79                 return;
80             }
81 
82             if (!extras.containsKey("StartTime")) {
83                 Log.e(TAG, "No Start Time specified");
84                 return;
85             }
86             str = extras.getString("StartTime");
87             Log.d(TAG, "Start Time = " + str);
88             startTime = Integer.valueOf(str);
89 
90 
91             if (!extras.containsKey("WriteTime")) {
92                 Log.e(TAG, "No WriteTime specified for GATT write");
93                 return;
94             }
95             str = extras.getString("WriteTime");
96             Log.d(TAG, "Write Time = " + str);
97             writeTime = Integer.valueOf(str);
98 
99             if (!extras.containsKey("IdleTime")) {
100                 Log.e(TAG, "No IdleTime specified for GATT write");
101                 return;
102             }
103             str = extras.getString("IdleTime");
104             Log.d(TAG, "Idle Time = " + str);
105             idleTime = Integer.valueOf(str);
106 
107             if (!extras.containsKey("Repetitions")) {
108                 Log.e(TAG, "No Repetitions specified for GATT write");
109                 return;
110             }
111             str = extras.getString("Repetitions");
112             Log.d(TAG, "Repetitions = " + str);
113             Repetitions = Integer.valueOf(str);
114 
115             mGattClientListener.startAlarm(startTime, writeTime, idleTime, Repetitions, null);
116         }
117     }
118 }
119