This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.js
301 lines (229 loc) · 8.36 KB
/
common.js
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
const { execSync } = require('child_process'),
{ readFileSync, writeFileSync } = require('fs'),
{ parseString } = require('xml2js'),
{ join } = require('path'),
{ randomFillSync } = require('crypto');
// Tizen Studio CLIs
const sdb = './tizen-studio/tools/sdb',
cli = './tizen-studio/tools/ide/bin/tizen';
// Check if a target device is connected
const target = exec(`${sdb} devices | awk '/:/ {print $1; exit}'`).trim();
if (!target) {
throw new Error('Failed to find a target device.');
}
// Check if a security profile is active
let active = '';
parseString(
readFileSync('./tizen-studio-data/profile/profiles.xml', {encoding: 'utf-8'}),
{
mergeAttrs: true,
explicitArray: false
},
(err, result) => {
if (err) {
throw new Error(err);
}
({ profiles: { active } } = result);
}
);
if (!active) {
throw new Error('Failed to find an active security profile.');
}
// Load db
const dbPath = './db.json',
db = {
rootfs: './rootfs/', // Default value
...JSON.parse(readFileSync(dbPath, {encoding: 'utf-8'}))
};
const localWd = './tmp/', // Working directory on the local machine
shName = 'dan_cmd.sh', // Batch command: file name
localSh = join(localWd, shName), // ... : file path
outName = 'dan_cmd.out', // Decompressed result: file name
localOut = join(localWd, outName), // ... : file path
gzName = 'dan_cmd.gz', // Compressed result: file name
localGz = join(localWd, gzName), // ... : file path
pjName = 'dan_cmd', // User::Pkg: project name
localPj = join(localWd, pjName), // ... : project directory
localC = join(localPj, 'src', `${pjName}.c`), // ... : source code file path
buildConf = 'Release', // ... : build configuration
localBuild = join(localPj, buildConf), // ... : build output path
pkgID = `org.example.${pjName}`, // ... : package id
pkgName = `${pkgID}-1.0.0-arm.tpk`; // ... : package file name
function exec() {
console.log([...arguments]);
const result = execSync.apply(this, arguments).toString();
console.log(result);
return result;
}
function clean() {
exec(`rm -rf ${localWd}`);
exec(`mkdir -p ${localWd}`);
}
async function connect() {
let test = '';
do {
try {
test = exec(`${sdb} -s "${target}" shell echo 1`);
} catch (e) {
console.error(e.message);
console.warn(`Waiting for the device ${target}...`);
await new Promise(cb => setTimeout(cb, 5000));
}
} while (test.trim() != '1');
}
function generateTag() {
let tag = randomFillSync(Buffer.alloc(12)).toString('base64');
tag = tag.replace(/\+/g, '0');
tag = tag.replace(/\//g, '0');
tag = tag.replace(/=/g, '0');
return tag;
}
module.exports = {
getData: (key) => {
if (key in db) {
return db[key];
} else {
throw new Error(`${key} not found in the database. Try executing the prior steps.`);
}
},
setData: (key, value) => {
db[key] = value;
writeFileSync(dbPath, JSON.stringify(db));
},
generateTag: generateTag,
// Run command as User::Shell
runAsShell: async cmd => {
// Clean localWd folder
clean();
// Connect to the target device
await connect();
// cmd should be always string
cmd += '';
// Create localSh
writeFileSync(localSh, cmd);
/**
* Working directory on the remote target
* A safe place that:
* - User::Shell can read/write
* - sdb can push/pull
*/
const remoteWd = '/opt/usr/home/owner/data/',
remoteSh = join(remoteWd, shName), // Batch command: file path
remoteOut = join(remoteWd, outName), // Decompressed result: file path
remoteGz = join(remoteWd, gzName); // Compressed result: file path
// Push localSh to remoteSh
exec(`${sdb} -s "${target}" push "${localSh}" "${remoteSh}"`);
// Execute remoteSh, output to remoteOut
exec(`${sdb} -s "${target}" shell bash -c 'sh "${remoteSh}" > "${remoteOut}" 2>&1'`);
// Gzip remoteOut into remoteGz
exec(`${sdb} -s "${target}" shell bash -c 'gzip -c "${remoteOut}" > "${remoteGz}"'`);
// Pull remoteGz to localGz
exec(`${sdb} -s "${target}" pull "${remoteGz}" "${localGz}"`);
// Remove all the remote files
exec(`${sdb} -s "${target}" shell rm -rf "${remoteSh}"`);
exec(`${sdb} -s "${target}" shell rm -rf "${remoteOut}"`);
exec(`${sdb} -s "${target}" shell rm -rf "${remoteGz}"`);
// Extract localGz into localOut
exec(`gzip -d -c "${gzName}" > "${outName}"`, {cwd: localWd});
// Read localOut
const res = readFileSync(localOut, {encoding: 'utf-8'});
// Clean localWd folder
clean();
return res;
},
// Run command as User::Pkg
runAsPkg: async (cmd, tag) => {
// Clean localWd folder
clean();
// Connect to the target device
await connect();
// cmd should be always string
cmd + '';
// Working directory: app_get_data_path()
const remoteWd = `/opt/usr/home/owner/apps_rw/${pkgID}/data/`,
remoteSh = join(remoteWd, shName), // Batch command: file path
remoteOut = join(remoteWd, outName), // Decompressed result: file path
remoteGz = join(remoteWd, gzName); // Compressed result: file path
// If not given, generate a random tag for dlog parsing
tag = tag || generateTag();
// Add control commands to cmd
cmd = cmd + `
# Gzip remoteOut into remoteGz
gzip -c "${remoteOut}" > "${remoteGz}";
# Signal the finish with remoteGz
echo -n -e '\\x03${tag}\\x00:>${remoteGz}\\x00' >> /dev/log_main;
`;
// Escape cmd; change `\` to `\\`
cmd = cmd.replace(/\\/g, '\\\\');
// Escape cmd; change `"` to `\"`
cmd = cmd.replace(/"/g, '\\"');
// Change `\n` to `"\n"`
cmd = cmd.replace(/\n/g, '\\n"\n"');
// Create the source code
const main = `
#include <stdio.h>
#include <stdlib.h>
#include <dlog.h>
#include <app_common.h>
#include <sys/types.h>
#include <unistd.h>
#define TAG "${tag}"
#define BUF_MAX 2048
const char *cmd = "${cmd}";
int main(void) {
dlog_print(DLOG_FATAL, TAG, "Launched! Check aliveness at /proc/%d", getpid());
// Create remoteSh
FILE *fp = fopen("${remoteSh}", "w");
fputs(cmd, fp);
fclose(fp);
// Execute remoteSh, output to remoteOut
system("bash -c 'bash \\"${remoteSh}\\" > \\"${remoteOut}\\" 2>&1'");
return 0;
}
`;
// Create a new project
exec(`${cli} create native-project -p wearable-3.0 -t basic-ui -n "${pjName}" -- "${localWd}"`);
// Write to the source code file
writeFileSync(localC, main);
// Build the project
exec(`${cli} build-native -a arm -C "${buildConf}" -- "${localPj}"`);
// Package the project
exec(`${cli} package -t tpk -S on -s "${active}" -- "${localBuild}"`);
// Try uninstalling the package first
try {
exec(`${cli} uninstall -p "${pkgID}" -s "${target}"`);
} catch (e) {}
// Install the package
exec(`${cli} install -n "${pkgName}" -s "${target}" -- "${localBuild}"`);
// Clear the entire dlog
exec(`${sdb} -s "${target}" dlog -c`);
// Run the package
exec(`${cli} run -p "${pkgID}" -s "${target}"`);
return new Promise(resolve => {
const wait = _ => {
console.log('Waiting for a completion signal from the process...');
// Fetch the log
const log = exec(`${sdb} -s "${target}" dlog -d -v raw ${tag}:* | tail -n 10`);
// Search for :>${remoteGz}
const [, remoteGz] = new RegExp(`:>(\/[^\n]+?${gzName})`).exec(log) || [];
if (remoteGz === undefined) {
setTimeout(wait, 5000);
return;
}
// Pull remoteGz to localGz
exec(`${sdb} -s "${target}" pull "${remoteGz}" "${localGz}"`);
// Extract localGz into localOut
exec(`gzip -d -c "${gzName}" > "${outName}"`, {cwd: localWd});
// Read localOut
const res = readFileSync(localOut, {encoding: 'utf-8'});
// Clean localWd folder
clean();
// Uninstall the package
exec(`${cli} uninstall -p "${pkgID}" -s "${target}"`);
// Return res
resolve(res);
}
wait();
});
}
};