This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libfm_jni.cpp
289 lines (228 loc) · 6.77 KB
/
libfm_jni.cpp
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
/*
* Copyright (C) 2020 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <jni.h>
#include <log/log.h>
#include <math.h>
#include "FmRadioController_slsi.h"
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "FMLIB_SLSI_JNI"
static FmRadioController_slsi * pFMRadio = NULL;
jboolean openDev(JNIEnv *env __unused, jobject thiz __unused)
{
pFMRadio = new FmRadioController_slsi();
if (pFMRadio->Initialise() == 0) {
ALOGI("%s FM Radio Initialized Successfully\n", __func__);
return JNI_TRUE;
}
ALOGE("%s failed to initialise FmRadio\n", __func__);
return JNI_FALSE;
}
jboolean closeDev(JNIEnv *env __unused, jobject thiz __unused)
{
delete pFMRadio;
pFMRadio = NULL;
ALOGI("%s FM Radio Un-Initialized Successfully\n", __func__);
return JNI_TRUE;
}
jboolean powerUp(JNIEnv *env __unused, jobject thiz __unused, jfloat freq)
{
pFMRadio->TuneChannel((int)(freq * 1000.00000000));
ALOGI("%s [freq=%d] \n", __func__, (int)freq);
return JNI_TRUE;
}
jboolean powerDown(JNIEnv *env __unused, jobject thiz __unused, jint type __unused)
{
ALOGI("%s \n", __func__);
return JNI_TRUE;
}
jboolean tune(JNIEnv *env __unused, jobject thiz __unused, jfloat freq)
{
pFMRadio->TuneChannel((int)(freq * 1000.00000000));
ALOGI("%s [freq=%d] \n", __func__, (int)freq);
return JNI_TRUE;
}
jfloat seek(JNIEnv *env __unused, jobject thiz __unused, jfloat freq __unused, jboolean isUp)
{
int ret;
if (isUp == JNI_TRUE) {
ret = pFMRadio->SeekUp();
} else {
ret = pFMRadio->SeekDown();
}
ALOGI("%s [freq=%d] \n", __func__, ret);
return roundf((ret/1000.00F) * 100) / 100;
}
jshortArray autoScan(JNIEnv *env __unused, jobject thiz __unused)
{
ALOGD("%s not supported \n", __func__);
return JNI_FALSE;
}
jshort readRds(JNIEnv *env __unused, jobject thiz __unused)
{
ALOGI("%s \n", __func__);
return pFMRadio->ReadRDS();
}
jbyteArray getPs(JNIEnv *env, jobject thiz __unused)
{
ALOGI("%s \n", __func__);
jbyteArray PSName;
ServiceName ps = pFMRadio->GetPs();
PSName = env->NewByteArray(ps.iLenght);
env->SetByteArrayRegion(PSName, 0, ps.iLenght, (const jbyte*)ps.Text);
return PSName;
}
jbyteArray getLrText(JNIEnv *env, jobject thiz __unused)
{
ALOGI("%s \n", __func__);
jbyteArray SName;
RadioText rt = pFMRadio->GetLrText();
SName = env->NewByteArray(rt.iLenght);
env->SetByteArrayRegion(SName, 0, rt.iLenght, (const jbyte*)rt.Text);
return SName;
}
jshort activeAf(JNIEnv *env __unused, jobject thiz __unused)
{
ALOGI("%s \n", __func__);
int ret = pFMRadio->GetChannel();
return roundf((ret/1000.00F) * 100) / 100;
}
jshortArray getAFList(JNIEnv *env __unused, jobject thiz __unused)
{
ALOGD("%s not supported \n", __func__);
return JNI_FALSE;
}
jint setRds(JNIEnv *env __unused, jobject thiz __unused, jboolean rdson)
{
if (rdson == JNI_TRUE) {
pFMRadio->EnableRDS();
} else {
pFMRadio->DisableRDS();
}
ALOGI("%s\n", __func__);
return JNI_TRUE;
}
jboolean stopScan(JNIEnv *env __unused, jobject thiz __unused)
{
ALOGD("%s not supported \n", __func__);
return JNI_TRUE;
}
jint setMute(JNIEnv *env __unused, jobject thiz __unused, jboolean mute)
{
if (mute == JNI_TRUE) {
pFMRadio->MuteOn();
} else {
pFMRadio->MuteOff();
}
ALOGI("%s [mute=%d] \n", __func__, (int)mute);
return JNI_TRUE;
}
jint isRdsSupport(JNIEnv *env __unused, jobject thiz __unused)
{
ALOGI("%s \n", __func__);
return JNI_TRUE;
}
jint switchAntenna(JNIEnv *env __unused, jobject thiz __unused, jint antenna __unused)
{
ALOGD("%s not supported \n", __func__);
return JNI_FALSE;
}
static const char *classPathNameRx = "com/android/fmradio/FmNative";
static JNINativeMethod methodsRx[] = {
{ "openDev", "()Z", (void*)openDev },
{ "closeDev", "()Z", (void*)closeDev },
{ "powerUp", "(F)Z", (void*)powerUp },
{ "powerDown", "(I)Z", (void*)powerDown },
{ "tune", "(F)Z", (void*)tune },
{ "seek", "(FZ)F", (void*)seek },
{ "autoScan", "()[S", (void*)autoScan },
{ "stopScan", "()Z", (void*)stopScan },
{ "setRds", "(Z)I", (void*)setRds },
{ "readRds", "()S", (void*)readRds },
{ "getPs", "()[B", (void*)getPs },
{ "getLrText", "()[B", (void*)getLrText },
{ "activeAf", "()S", (void*)activeAf },
{ "setMute", "(Z)I", (void*)setMute },
{ "isRdsSupport", "()I", (void*)isRdsSupport },
{ "switchAntenna", "(I)I", (void*)switchAntenna },
};
/*
* Register several native methods for one class.
*/
static jint registerNativeMethods(JNIEnv* env, const char* className,
JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = env->FindClass(className);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
if (clazz == NULL) {
ALOGE("Native registration unable to find class '%s'", className);
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
ALOGE("RegisterNatives failed for '%s'", className);
return JNI_FALSE;
}
ALOGD("%s, success\n", __func__);
return JNI_TRUE;
}
/*
* Register native methods for all classes we know about.
*
* returns JNI_TRUE on success.
*/
static jint registerNatives(JNIEnv* env)
{
jint ret = JNI_FALSE;
if (registerNativeMethods(env, classPathNameRx,methodsRx,
sizeof(methodsRx) / sizeof(methodsRx[0]))) {
ret = JNI_TRUE;
}
ALOGD("%s, done\n", __func__);
return ret;
}
// ----------------------------------------------------------------------------
/*
* This is called by the VM when the shared library is first loaded.
*/
typedef union {
JNIEnv* env;
void* venv;
} UnionJNIEnvToVoid;
jint JNI_OnLoad(JavaVM* vm, void* reserved __unused)
{
UnionJNIEnvToVoid uenv;
uenv.venv = NULL;
jint result = -1;
JNIEnv* env = NULL;
ALOGI("JNI_OnLoad");
if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("ERROR: GetEnv failed");
goto fail;
}
env = uenv.env;
if (registerNatives(env) != JNI_TRUE) {
ALOGE("ERROR: registerNatives failed");
goto fail;
}
result = JNI_VERSION_1_4;
fail:
return result;
}