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         Log.d(TAG, "Start GattPMCReceiver()");
53     }
54 
55     /**
56      * Method to receive the broadcast from python client for PMC commands
57      *
58      * @param context - system will provide a context to this function
59      * @param intent - system will provide an intent to this function
60      */
61     @Override
onReceive(Context context, Intent intent)62     public void onReceive(Context context, Intent intent) {
63         Log.d(TAG, "Intent: " + intent.getAction());
64         if (intent.getAction().equals(GATTPMC_INTENT)) {
65             Bundle extras = intent.getExtras();
66             int startTime = 0, writeTime = 0, idleTime = 0, Repetitions = 1;
67             String str;
68 
69             if (extras == null) {
70                 Log.e(TAG, "No parameters specified");
71                 return;
72             }
73 
74             if (extras.containsKey("GattServer")) {
75                 // this is for Gatt Server
76                 Log.d(TAG, "For Gatt Server");
77                 mGattServer.startGattServer();
78                 return;
79             }
80 
81             if (!extras.containsKey("StartTime")) {
82                 Log.e(TAG, "No Start Time specified");
83                 return;
84             }
85             str = extras.getString("StartTime");
86             Log.d(TAG, "Start Time = " + str);
87             startTime = Integer.valueOf(str);
88 
89 
90             if (!extras.containsKey("WriteTime")) {
91                 Log.e(TAG, "No WriteTime specified for GATT write");
92                 return;
93             }
94             str = extras.getString("WriteTime");
95             Log.d(TAG, "Write Time = " + str);
96             writeTime = Integer.valueOf(str);
97 
98             if (!extras.containsKey("IdleTime")) {
99                 Log.e(TAG, "No IdleTime specified for GATT write");
100                 return;
101             }
102             str = extras.getString("IdleTime");
103             Log.d(TAG, "Idle Time = " + str);
104             idleTime = Integer.valueOf(str);
105 
106             if (!extras.containsKey("Repetitions")) {
107                 Log.e(TAG, "No Repetitions specified for GATT write");
108                 return;
109             }
110             str = extras.getString("Repetitions");
111             Log.d(TAG, "Repetitions = " + str);
112             Repetitions = Integer.valueOf(str);
113 
114             mGattClientListener.startAlarm(startTime, writeTime, idleTime, Repetitions, null);
115         }
116     }
117 }
118