Just cast it to
override fun httpErrorHandle(e: Throwable) {
val error = e as ANError
errorMessage(activity!!, error)
}
val error = e as ANError
showErrorMessage(activity, error.errorCode)
HTTP status code below function check
Pass argument - Context and HTTP status code
fun showErrorMessage(activity: Activity, Code: Int) {
when (Code) {
0 -> showCustomToastCode(activity, activity.getString(R.string.code_500))
100 -> showCustomToastCode(activity, activity.getString(R.string.code_100))
101 -> showCustomToastCode(activity, activity.getString(R.string.code_101))
103 -> showCustomToastCode(activity, activity.getString(R.string.code_103))
200 -> showCustomToastCode(activity, activity.getString(R.string.code_200))
201 -> showCustomToastCode(activity, activity.getString(R.string.code_201))
202 -> showCustomToastCode(activity, activity.getString(R.string.code_202))
203 -> showCustomToastCode(activity, activity.getString(R.string.code_203))
204 -> showCustomToastCode(activity, activity.getString(R.string.code_204))
205 -> showCustomToastCode(activity, activity.getString(R.string.code_205))
206 -> showCustomToastCode(activity, activity.getString(R.string.code_206))
300 -> showCustomToastCode(activity, activity.getString(R.string.code_300))
301 -> showCustomToastCode(activity, activity.getString(R.string.code_301))
302 -> showCustomToastCode(activity, activity.getString(R.string.code_302))
303 -> showCustomToastCode(activity, activity.getString(R.string.code_303))
304 -> showCustomToastCode(activity, activity.getString(R.string.code_304))
307 -> showCustomToastCode(activity, activity.getString(R.string.code_307))
308 -> showCustomToastCode(activity, activity.getString(R.string.code_308))
400 -> showCustomToastCode(activity, activity.getString(R.string.code_400))
401 -> {
unAuthorizedPopUpView(activity)
}
402 -> showCustomToastCode(activity, activity.getString(R.string.code_402))
403 -> showCustomToastCode(activity, activity.getString(R.string.code_403))
404 -> showCustomToastCode(activity, activity.getString(R.string.code_404))
405 -> showCustomToastCode(activity, activity.getString(R.string.code_405))
406 -> showCustomToastCode(activity, activity.getString(R.string.code_406))
407 -> showCustomToastCode(activity, activity.getString(R.string.code_407))
408 -> showCustomToastCode(activity, activity.getString(R.string.code_408))
409 -> showCustomToastCode(activity, activity.getString(R.string.code_409))
410 -> showCustomToastCode(activity, activity.getString(R.string.code_410))
411 -> showCustomToastCode(activity, activity.getString(R.string.code_411))
412 -> showCustomToastCode(activity, activity.getString(R.string.code_412))
413 -> showCustomToastCode(activity, activity.getString(R.string.code_413))
414 -> showCustomToastCode(activity, activity.getString(R.string.code_414))
415 -> showCustomToastCode(activity, activity.getString(R.string.code_415))
416 -> showCustomToastCode(activity, activity.getString(R.string.code_416))
417 -> showCustomToastCode(activity, activity.getString(R.string.code_417))
418 -> showCustomToastCode(activity, activity.getString(R.string.code_418))
422 -> showCustomToastCode(activity, activity.getString(R.string.code_422))
425 -> showCustomToastCode(activity, activity.getString(R.string.code_425))
426 -> showCustomToastCode(activity, activity.getString(R.string.code_426))
428 -> showCustomToastCode(activity, activity.getString(R.string.code_428))
429 -> showCustomToastCode(activity, activity.getString(R.string.code_429))
431 -> showCustomToastCode(activity, activity.getString(R.string.code_431))
451 -> showCustomToastCode(activity, activity.getString(R.string.code_451))
500 -> showCustomToastCode(activity, activity.getString(R.string.code_500))
// 501 -> showCustomToastCode(activity, activity.getString(R.string.code_501))
501 -> {
invalidJWTTokenAuth(activity)
}
502 -> showCustomToastCode(activity, activity.getString(R.string.code_502))
503 -> showCustomToastCode(activity, activity.getString(R.string.code_503))
504 -> showCustomToastCode(activity, activity.getString(R.string.code_504))
505 -> showCustomToastCode(activity, activity.getString(R.string.code_505))
506 -> showCustomToastCode(activity, activity.getString(R.string.code_506))
507 -> showCustomToastCode(activity, activity.getString(R.string.code_507))
508 -> showCustomToastCode(activity, activity.getString(R.string.code_508))
511 -> showCustomToastCode(activity, activity.getString(R.string.code_511))
}
}
HTTP String message
<string name="code_100">Continue</string>
<string name="code_101">Switching Protocols</string>
<string name="code_103">Early Hints</string>
<string name="code_200">OK</string>
<string name="code_201">Created</string>
<string name="code_202">Accepted</string>
<string name="code_203">Non-Authoritative Information</string>
<string name="code_204">Data Not Available.!</string>
<string name="code_205">Reset Content</string>
<string name="code_206">Partial Content</string>
<string name="code_300">Multiple Choices</string>
<string name="code_301">Moved Permanently</string>
<string name="code_302">Found</string>
<string name="code_303">See Other</string>
<string name="code_304">Not Modified</string>
<string name="code_307">Temporary Redirect</string>
<string name="code_308">Permanent Redirect</string>
<string name="code_400">Bad Request</string>
<string name="code_402">Payment Required</string>
<string name="code_403">Forbidden</string>
<string name="code_404">Data Not Available.!</string>
<string name="code_405">Method Not Allowed</string>
<string name="code_406">Not Acceptable</string>
<string name="code_407">Proxy Authentication Required</string>
<string name="code_408">Request Timeout</string>
<string name="code_409">already exists.</string>
<string name="code_410">Gone</string>
<string name="code_411">Length Required</string>
<string name="code_412">Precondition Failed</string>
<string name="code_413">Payload Too Large</string>
<string name="code_414">URI Too Long</string>
<string name="code_415">Unsupported Media Type</string>
<string name="code_416">Range Not Satisfiable</string>
<string name="code_417">Expectation Failed</string>
<string name="code_418">I'm a teapot</string>
<string name="code_422">Unprocessable Entity</string>
<string name="code_425">Too Early</string>
<string name="code_426">Upgrade Required</string>
<string name="code_428">Precondition Required</string>
<string name="code_429">Too Many Requests</string>
<string name="code_431">Request Header Fields Too Large</string>
<string name="code_451">Unavailable For Legal Reasons</string>
<string name="code_500">Fail to connect server.</string>
<string name="code_501">Not implemented</string>
<string name="code_502">Bad Gateway</string>
<string name="code_503">Service Unavailable.</string>
<string name="code_504">Gateway Timeout</string>
<string name="code_505">HTTP Version Not Supported</string>
<string name="code_506">Variant Also Negotiates</string>
<string name="code_507">Insufficient Storage</string>
<string name="code_508">Loop Detected</string>
<string name="code_511">Network Authentication Required</string>
401 Status code Display Popus Unauthorizedmessage
fun unAuthorizedPopUpView(activity: Activity?) {
// Handle your logic for 401 (Unauthorized)
}
fun invalidJWTTokenAuth(activity: Activity?) {
// Handle your logic for 501 (Not implemented)
}
Related
I have a application that I got froma freelancing team that uses classical Bluetooth in data exchange. I am in the process of switching it from classical Bluetooth to BLE.
Now there are different fragments each with their own viewmodels, viewmodel factories and their repositories that acts as an intermediary between the viewmodel and the DataStore class that hosts the data.
I was following a guide and what I achieved so far is scanning for BLE devices and connecting to them to display the characteristics and their properties (Readable, Writable, Indicatable, Notifiable). I have an activity that scans for devices and then upon selecting a device it goes to another activity, connects to the device and displays the characteristics.
Different Parameters are symbolized with codes likes these:
enum class ReadRequestCodes(val value:String) {
KEY_ADDRESS ("08 00 00 00 20 30 05 11 00 00 00 00 00"),
TOOL_ADDRESS ("08 00 00 00 20 30 05 27 00 00 00 00 00"),
RPM_THRESHOLD("08 00 00 00 20 30 05 13 00 00 00 00 00"),
BACKLASH ("08 00 00 00 20 30 05 22 00 00 00 00 00"),
POWER_SRC_TYPE ("08 00 00 00 20 30 05 26 00 00 00 00 00"),
BATTERY1_PERCENTAGE("08 00 00 00 20 30 11 00 00 00 00 00 00"),
BATTERY2_PERCENTAGE("08 00 00 00 20 30 12 00 00 00 00 00 00"),
HOME_POSITION ("08 00 00 00 20 30 05 15 00 00 00 00 00"),
BYPASS_POSITION ("08 00 00 00 20 30 05 17 00 00 00 00 00"),
HC_POSITION ("08 00 00 00 20 30 05 19 00 00 00 00 00"),
ISOLATION_POSITION("08 00 00 00 20 30 05 1B 00 00 00 00 00"),
PRESSURE_SENSOR_GAIN("08 00 00 00 20 30 05 2B 00 00 00 00 00"),
PRESSURE_SENSOR_OFFSET("08 00 00 00 20 30 05 2C 00 00 00 00 00"),
PRESSURE_SENSOR_RANGE("08 00 00 00 20 30 05 2A 00 00 00 00 00"),
PRESSURE_SENSOR_EXCITATION("08 00 00 00 20 30 05 29 00 00 00 00 00"),
PRESSURE_SENSOR_SERIAL("08 00 00 00 20 30 05 2D 00 00 00 00 00"),
}
there are enums for read requests, write commands, read response.
Now in the fragments previously mentioned there are buttons that are supposed to initiate a request, basically you click and it requests the parameter that corresponds to one of the codes like in the snippet and acquires the value from the device I'm connected to. In other cases I type in data and send it to the device, and the parameter is symbolized with a code similar to the one in the snippet but for write commands.
Now the issue here is HOW to do I exchange data? The code below is form my call back function when I connect to a device:
private val gattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
val deviceAddress = gatt.device.address
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Timber.w("onConnectionStateChange: connected to $deviceAddress")
deviceGattMap[gatt.device] = gatt
Handler(Looper.getMainLooper()).post {
gatt.discoverServices()
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Timber.e("onConnectionStateChange: disconnected from $deviceAddress")
teardownConnection(gatt.device)
}
} else {
Timber.e("onConnectionStateChange: status $status encountered for $deviceAddress!")
if (pendingOperation is Connect) {
signalEndOfOperation()
}
teardownConnection(gatt.device)
}
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
with(gatt) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Timber.w("Discovered ${services.size} services for ${device.address}.")
printGattTable()
requestMtu(device, GATT_MAX_MTU_SIZE)
listeners.forEach { it.get()?.onConnectionSetupComplete?.invoke(this) }
} else {
Timber.e("Service discovery failed due to status $status")
teardownConnection(gatt.device)
}
}
if (pendingOperation is Connect) {
signalEndOfOperation()
}
}
override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
Timber.w("ATT MTU changed to $mtu, success: ${status == BluetoothGatt.GATT_SUCCESS}")
listeners.forEach { it.get()?.onMtuChanged?.invoke(gatt.device, mtu) }
if (pendingOperation is MtuRequest) {
signalEndOfOperation()
}
}
override fun onCharacteristicRead(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
with(characteristic) {
when (status) {
BluetoothGatt.GATT_SUCCESS -> {
Timber.i("Read characteristic $uuid | value: ${value.toHexString()}")
listeners.forEach {
it.get()?.onCharacteristicRead?.invoke(
gatt.device,
this
)
}
}
BluetoothGatt.GATT_READ_NOT_PERMITTED -> {
Timber.e("Read not permitted for $uuid!")
}
else -> {
Timber.e("Characteristic read failed for $uuid, error: $status")
}
}
}
if (pendingOperation is CharacteristicRead) {
signalEndOfOperation()
}
}
override fun onCharacteristicWrite(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
with(characteristic) {
when (status) {
BluetoothGatt.GATT_SUCCESS -> {
Timber.i("Wrote to characteristic $uuid | value: ${value.toHexString()}")
listeners.forEach {
it.get()?.onCharacteristicWrite?.invoke(
gatt.device,
this
)
}
}
BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> {
Timber.e("Write not permitted for $uuid!")
}
else -> {
Timber.e("Characteristic write failed for $uuid, error: $status")
}
}
}
if (pendingOperation is CharacteristicWrite) {
signalEndOfOperation()
}
}
override fun onCharacteristicChanged(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic
) {
with(characteristic) {
Timber.i("Characteristic $uuid changed | value: ${value.toHexString()}")
listeners.forEach { it.get()?.onCharacteristicChanged?.invoke(gatt.device, this) }
}
}
override fun onDescriptorRead(
gatt: BluetoothGatt,
descriptor: BluetoothGattDescriptor,
status: Int
) {
with(descriptor) {
when (status) {
BluetoothGatt.GATT_SUCCESS -> {
Timber.i("Read descriptor $uuid | value: ${value.toHexString()}")
listeners.forEach { it.get()?.onDescriptorRead?.invoke(gatt.device, this) }
}
BluetoothGatt.GATT_READ_NOT_PERMITTED -> {
Timber.e("Read not permitted for $uuid!")
}
else -> {
Timber.e("Descriptor read failed for $uuid, error: $status")
}
}
}
if (pendingOperation is DescriptorRead) {
signalEndOfOperation()
}
}
override fun onDescriptorWrite(
gatt: BluetoothGatt,
descriptor: BluetoothGattDescriptor,
status: Int
) {
with(descriptor) {
when (status) {
BluetoothGatt.GATT_SUCCESS -> {
Timber.i("Wrote to descriptor $uuid | value: ${value.toHexString()}")
if (isCccd()) {
onCccdWrite(gatt, value, characteristic)
} else {
listeners.forEach {
it.get()?.onDescriptorWrite?.invoke(
gatt.device,
this
)
}
}
}
BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> {
Timber.e("Write not permitted for $uuid!")
}
else -> {
Timber.e("Descriptor write failed for $uuid, error: $status")
}
}
}
if (descriptor.isCccd() &&
(pendingOperation is EnableNotifications || pendingOperation is DisableNotifications)
) {
signalEndOfOperation()
} else if (!descriptor.isCccd() && pendingOperation is DescriptorWrite) {
signalEndOfOperation()
}
}
private fun onCccdWrite(
gatt: BluetoothGatt,
value: ByteArray,
characteristic: BluetoothGattCharacteristic
) {
val charUuid = characteristic.uuid
val notificationsEnabled =
value.contentEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE) ||
value.contentEquals(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)
val notificationsDisabled =
value.contentEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)
when {
notificationsEnabled -> {
Timber.w("Notifications or indications ENABLED on $charUuid")
listeners.forEach {
it.get()?.onNotificationsEnabled?.invoke(
gatt.device,
characteristic
)
}
}
notificationsDisabled -> {
Timber.w("Notifications or indications DISABLED on $charUuid")
listeners.forEach {
it.get()?.onNotificationsDisabled?.invoke(
gatt.device,
characteristic
)
}
}
else -> {
Timber.e("Unexpected value ${value.toHexString()} on CCCD of $charUuid")
}
}
}
}
This was from the activity I displayed the characteristics in:
private val connectionEventListener by lazy {
ConnectionEventListener().apply {
onDisconnect = {
runOnUiThread {
alert {
title = "Disconnected"
message = "Disconnected from device."
positiveButton("OK") { onBackPressed() }
}.show()
}
}
onCharacteristicRead = { _, characteristic ->
Timber.i("Read from ${characteristic.uuid}: ${characteristic.value.toHexString()}")
}
onCharacteristicWrite = { _, characteristic ->
Timber.i("Wrote to: ${characteristic.uuid}")
}
onMtuChanged = { _, mtu ->
Timber.i("MTU updated to: $mtu")
}
onCharacteristicChanged = { _, characteristic ->
Timber.i("Value changed on ${characteristic.uuid}: ${characteristic.value.toHexString()}")
}
onNotificationsEnabled = { _, characteristic ->
Timber.i("Notify enabled on: ${characteristic.uuid}")
notifyingCharacteristics.add(characteristic.uuid)
}
onNotificationsDisabled = { _, characteristic ->
Timber.i("Notify disabled on: ${characteristic.uuid}")
notifyingCharacteristics.remove(characteristic.uuid)
}
}
}
I don't know what else I need to post here but I can edit if the need arises. The point here I am stuck and don't know how to proceed. If I am able to connect to the BLE device and display its characteristics and all that, how do I get it to send me data or send to it data? How do I go from here?
I am facing a problem with coding my first Android-App.
I want to build the login-system of the app around my existing webserver/webinterface.
I am using the Fuel-Library, and as far as I can tell, the GET Requests are working fine.
The problem is the response. When I print it out, everything is see is some information about the request itself, but the printed echo from PHP isn't showing up anywhere.
Response printed out:
I/System.out: <-- 200 https://...hidden :)
I/System.out: Response : OK
Length : -1
Body : test
Headers : (11)
Connection : Keep-Alive
Date : Mon, 30 Mar 2020 18:06:39 GMT
X-Android-Selected-Protocol : http/1.1
Server : Apache
X-Powered-By : PHP/7.3.5, PleskLin
Content-Type : text/html; charset=UTF-8
X-Android-Received-Millis : 1585591597000
Vary : Accept-Encoding
X-Android-Response-Source : NETWORK 200
X-Android-Sent-Millis : 1585591596960
Keep-Alive : timeout=5, max=100
The same is happening with POST Requests.
Here is my Kotlin-Code:
val url = "https://myserver.com/testlogin.php?username=".plus(username.toString()).plus("&password=").plus(password.toString())
url.httpGet().responseString{
request, response, result ->
Toast.makeText(this#MainActivity, result.toString(), Toast.LENGTH_LONG).show()
}
And the PHP Code on the Webserver:
<?php $username = $_GET["username"]; $password = $_GET["password"]; echo $username; ?>
I am searching for more than 7 hours now. send help
Try this
url.httpGet().responseString { request, response, result ->
when (result) {
is Result.Failure -> {
val ex = result.getException()
println(ex)
}
is Result.Success -> {
val data = result.get()
println(data)
}
}
}
Official documentation
I just found the problem:
val data = result.get() println(data)
prints the response string of the php file.
My android app passes in an OpenGL texture2D to my OpenCL kernel, however the pixels values being read by my kernel are out of bounds (>255).
I create my OpenGL texture like this:
GLES20.glGenTextures ( 2, targetTex, 0 );
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, targetTex[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, image_width, image_height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
The texture is then rendered to by binding it with a FBO:
targetFramebuffer = IntBuffer.allocate(1);
GLES20.glGenFramebuffers(1, targetFramebuffer);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, targetFramebuffer.get(0));
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, targetTex[0], 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
I create the cl memory object like so:
mem_images[0] = clCreateFromGLTexture2D(m_clContext, CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, in_tex, &err);
and this is my OpenCL kernel:
const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;
kernel void transfer_data(__read_only image2d_t input_image, __global float* debug) {
int2 pos;
uint4 pixel;
for (pos.y = get_global_id(1); pos.y < HEIGHT; pos.y += get_global_size(1)) {
for (pos.x = get_global_id(0); pos.x < WIDTH; pos.x += get_global_size(0)) {
pixel = read_imageui(input_image, sampler, pos);
debug[(pos.x + pos.y*WIDTH)*NUM_CHANNELS + 0] = pixel.x;
debug[(pos.x + pos.y*WIDTH)*NUM_CHANNELS + 1] = pixel.y;
debug[(pos.x + pos.y*WIDTH)*NUM_CHANNELS + 2] = pixel.z;
}
}
}
This is how I am enqueing the kernel:
local2Dsize[0] = 4;
local2Dsize[1] = 4;
global2Dsize[0] = clamp(image_width, 0, max_work_items[0]);
global2Dsize[1] = clamp(image_height, 0, max_work_items[1]);
global2Dsize[0] = ceil((float)global2Dsize[0]/(float)local2Dsize[0])*local2Dsize[0];
global2Dsize[1] = ceil((float)global2Dsize[1]/(float)local2Dsize[1])*local2Dsize[1];
twoDlocal_sizes["transfer_data"] = local2Dsize;
twoDglobal_sizes["transfer_data"] = global2Dsize;
kernels["transfer_data"] = clCreateKernel(m_program, "transfer_data", &err);
err = clSetKernelArg(kernels["transfer_data"], 0, sizeof(cl_mem), &mem_images[0]);
err |= clSetKernelArg(kernels["transfer_data"], 1, sizeof(cl_mem), &mems["image"]);
err = clEnqueueAcquireGLObjects(m_queue, 1, &mem_images[0], 0, 0, 0);
CHECK_ERROR_OCL(err, "acquiring GL objects", return false);
err = clEnqueueNDRangeKernel(m_queue, kernels["transfer_data"], 2, NULL, twoDglobal_sizes["transfer_data"], twoDlocal_sizes["transfer_data"], 0, NULL, NULL);
err = clFinish(m_queue);
err = clEnqueueReleaseGLObjects(m_queue, 1, &mem_images[0], 0, 0, 0);
CHECK_ERROR_OCL(err, "releasing GL objects", return false);
Now back on host when I print out these pixel values (from the array debug), they are way out of bounds and I don't understand why that is the case.
If you need more insight:
The overall aim of my project is to obtain camera frames in form of an OpenGL texture, process them using OpenCL and render the output back to the screen.
However the texture obtained from android camera can only be bound to GL_TEXTURE_EXTERNAL_OES (http://developer.android.com/reference/android/graphics/SurfaceTexture.html), and this is not a valid texture to create an OpenCL memory object from.
Therefore I am rendering the camera output to a GL_TEXTURE_2D and passing that to OpenCL.
I am sure that the pixels are being rendered to the texture correctly, because when I display the texture on the screen (without any OpenCL involved) it displays the image properly.
I did some debugging by creating a texture (as opposed to getting data from the camera) and passing that to opencl. So these are the mappings I get:
0 -> 0
1 -> 7172
2 -> 8196
3 -> 8710
4 -> 9220
5 -> 9477
6 -> 9734
7 -> 9991
8 -> 10244
9 -> 10372
10 -> 10501
11 -> 10629
12 -> 10758
13 -> 10886
14 -> 11015
15 -> 11143
16 -> 11268
17 -> 11332
18 -> 11396
19 -> 11460
20 -> 11525
21 -> 11589
22 -> 11653
23 -> 11717
24 -> 11782
25 -> 11846
26 -> 11910
27 -> 11974
28 -> 12039
29 -> 12103
30 -> 12167
31 -> 12231
32 -> 12292
33 -> 12324
34 -> 12356
35 -> 12388
36 -> 12420
37 -> 12452
38 -> 12484
39 -> 12516
40 -> 12549
41 -> 12581
42 -> 12613
43 -> 12645
44 -> 12677
45 -> 12709
46 -> 12741
47 -> 12773
48 -> 12806
49 -> 12838
50 -> 12870
51 -> 12902
52 -> 12934
53 -> 12966
54 -> 12998
55 -> 13030
56 -> 13063
57 -> 13095
58 -> 13127
59 -> 13159
60 -> 13191
61 -> 13223
62 -> 13255
63 -> 13287
64 -> 13316
65 -> 13332
66 -> 13348
67 -> 13364
68 -> 13380
69 -> 13396
70 -> 13412
71 -> 13428
72 -> 13444
73 -> 13460
74 -> 13476
75 -> 13492
76 -> 13508
77 -> 13524
78 -> 13540
79 -> 13556
80 -> 13573
81 -> 13589
82 -> 13605
83 -> 13621
84 -> 13637
85 -> 13653
86 -> 13669
87 -> 13685
88 -> 13701
89 -> 13717
90 -> 13733
91 -> 13749
92 -> 13765
93 -> 13781
94 -> 13797
95 -> 13813
96 -> 13830
97 -> 13846
98 -> 13862
99 -> 13878
100 -> 13894
101 -> 13910
102 -> 13926
103 -> 13942
104 -> 13958
105 -> 13974
106 -> 13990
107 -> 14006
108 -> 14022
109 -> 14038
110 -> 14054
111 -> 14070
112 -> 14087
113 -> 14103
114 -> 14119
115 -> 14135
116 -> 14151
117 -> 14167
118 -> 14183
119 -> 14199
120 -> 14215
121 -> 14231
122 -> 14247
123 -> 14263
124 -> 14279
125 -> 14295
126 -> 14311
127 -> 14327
128 -> 14340
129 -> 14348
130 -> 14356
131 -> 14364
132 -> 14372
133 -> 14380
134 -> 14388
135 -> 14396
136 -> 14404
137 -> 14412
138 -> 14420
139 -> 14428
140 -> 14436
141 -> 14444
142 -> 14452
143 -> 14460
144 -> 14468
145 -> 14476
146 -> 14484
147 -> 14492
148 -> 14500
149 -> 14508
150 -> 14516
151 -> 14524
152 -> 14532
153 -> 14540
154 -> 14548
155 -> 14556
156 -> 14564
157 -> 14572
158 -> 14580
159 -> 14588
160 -> 14597
161 -> 14605
162 -> 14613
163 -> 14621
164 -> 14629
165 -> 14637
166 -> 14645
167 -> 14653
168 -> 14661
169 -> 14669
170 -> 14677
171 -> 14685
172 -> 14693
173 -> 14701
174 -> 14709
175 -> 14717
176 -> 14725
177 -> 14733
178 -> 14741
179 -> 14749
180 -> 14757
181 -> 14765
182 -> 14773
183 -> 14781
184 -> 14789
185 -> 14797
186 -> 14805
187 -> 14813
188 -> 14821
189 -> 14829
190 -> 14837
191 -> 14845
192 -> 14854
193 -> 14862
194 -> 14870
195 -> 14878
196 -> 14886
197 -> 14894
198 -> 14902
199 -> 14910
200 -> 14918
201 -> 14926
202 -> 14934
203 -> 14942
204 -> 14950
205 -> 14958
206 -> 14966
207 -> 14974
208 -> 14982
209 -> 14990
210 -> 14998
211 -> 15006
212 -> 15014
213 -> 15022
214 -> 15030
215 -> 15038
216 -> 15046
217 -> 15054
218 -> 15062
219 -> 15070
220 -> 15078
221 -> 15086
222 -> 15094
223 -> 15102
224 -> 15111
225 -> 15119
226 -> 15127
227 -> 15135
228 -> 15143
229 -> 15151
230 -> 15159
231 -> 15167
232 -> 15175
233 -> 15183
234 -> 15191
235 -> 15199
236 -> 15207
237 -> 15215
238 -> 15223
239 -> 15231
240 -> 15239
241 -> 15247
242 -> 15255
243 -> 15263
244 -> 15271
245 -> 15279
246 -> 15287
247 -> 15295
248 -> 15303
249 -> 15311
250 -> 15319
251 -> 15327
252 -> 15335
253 -> 15343
254 -> 15351
255 -> 15359
On the left is the colour value that I input in the OpenGL texture and on the left is the corresponding value I get when I read the values in OpenCL.
The GL_UNSIGNED_BYTE texture is being mapped to OpenCL as CL_UNORM_INT8. You need to use read_imagef to read from these images rather than read_imageui. The values you are seeing when you use read_imageui are the raw bits of the internal floating point format.
You are not acquiring and releasing the GL objects before accessing them. This causes the kernel not to read to data in the GL internal buffer but a local CL copy of it.
Proper code: (BTW you should check those "err" values for errors)
local2Dsize[0] = 4;
local2Dsize[1] = 4;
global2Dsize[0] = clamp(image_width, 0, max_work_items[0]);
global2Dsize[1] = clamp(image_height, 0, max_work_items[1]);
global2Dsize[0] = ceil((float)global2Dsize[0]/(float)local2Dsize[0])*local2Dsize[0];
global2Dsize[1] = ceil((float)global2Dsize[1]/(float)local2Dsize[1])*local2Dsize[1];
twoDlocal_sizes["transfer_data"] = local2Dsize;
twoDglobal_sizes["transfer_data"] = global2Dsize;
kernels["transfer_data"] = clCreateKernel(m_program, "transfer_data", &err);
err = clSetKernelArg(kernels["transfer_data"], 0, sizeof(cl_mem), &mem_images[0]);
err |= clSetKernelArg(kernels["transfer_data"], 1, sizeof(cl_mem), &mems["image"]);
err = clEnqueueAcquireGLObjects(m_queue, 1, mem_images, NULL, NULL, NULL);
err = clEnqueueNDRangeKernel(m_queue, kernels["transfer_data"], 2, NULL, twoDglobal_sizes["transfer_data"], twoDlocal_sizes["transfer_data"], 0, NULL, NULL);
err = clEnqueueReleaseGLObjects (m_queue, 1, mem_images, NULL, NULL, NULL);
err = clFinish(m_queue);
Okay so I think it's a bug in the OpenCL implementation.
With a bit of help from Wolfram Alpha, I created a function to reverse the above mappings and obtain values in the range of 0 and 255.
float GL_to_CL(uint val) {
if (val >= 14340) return round(0.1245790*val - 1658.44); //>=128
if (val >= 13316) return round(0.0622869*val - 765.408); //>=64
if (val >= 12292) return round(0.0311424*val - 350.800); //>=32
if (val >= 11268) return round(0.0155702*val - 159.443); //>=16
float v = (float) val;
return round(0.0000000000000125922*pow(v,4.f) - 0.00000000026729*pow(v,3.f) + 0.00000198135*pow(v,2.f) - 0.00496681*v - 0.0000808829);
}
So the GL_to_CL() is a combination of 4 linear functions and a Quartic function.
I tried creating just a single function using polynomial interpolation, however the degree of the polynomial was too large and therefore more computationally expensive to solve than the combination of the 5 functions proposed above.
Another alternate is to use a 15k sized array to achieve constant time, however that would require me uploading roughly 15k Bytes to GPU's global memory.
Considering I am using the kernels to do image processing, I would be slightly pushing it.
Also accessing from global memory in OpenCL is often more expensive than performing some simple calculations.
I have xml file in res/xml and I want to change one node just for building and after that go beck to default value, for example:
before:
<resources>
<integer name="ga_sessionTimeout">
300
</integer>
<bool name="ga_autoActivityTracking">
true
</bool>
<screenName name="pl.grzeslowski.transport.activities.MainActivity_">
Main Activity
</screenName>
<screenName name="pl.grzeslowski.transport.activities.ResultActivity_">
Result Activity
</screenName>
<string name="ga_trackingId">
UA-XXXXXXX-Y
</string>
</resources>
After:
<resources>
<integer name="ga_sessionTimeout">
300
</integer>
<bool name="ga_autoActivityTracking">
true
</bool>
<screenName name="pl.grzeslowski.transport.activities.MainActivity_">
Main Activity
</screenName>
<screenName name="pl.grzeslowski.transport.activities.ResultActivity_">
Result Activity
</screenName>
<string name="ga_trackingId">
UA-1111111-11111
</string>
</resources>
In my gradle .build I have this task (on the bottom of file) but it doesn't work:
task loadAdSense() {
println "load"
// saving adsense tag to xml file
def xmlFile = file("src/main/res/xml/global_tracker.xml")
def xml = new XmlParser().parse(xmlFile)
xml.string.each {
if (it.#name == "ga_trackingId") {
it.value = adsenseTag
}
}
new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
}
build.dependsOn loadAdSense
task cleanXml(dependsOn: build) << {
println "clean"
// going back with xml
def xmlFile = file("src/main/res/xml/global_tracker.xml")
def xml = new XmlParser().parse(xmlFile)
xml.string.each {
if (it.#name == "ga_trackingId") {
it.value = "UA-XXXXXXX-Y"
}
}
new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
}
cleanXml.dependsOn build
cleanXml.mustRunAfter build
Description: I want to connect to server via POST request. My method, which posts the request:
function loginClick(e) {
var url = "http://...";
var xhr = Ti.Network.createHTTPClient({
onload: function (e) { // this function is called when data is returned from the server and available for use
// this.responseText holds the raw text return of the message (used for text/JSON)
// this.responseXML holds any returned XML (including SOAP)
// this.responseData holds any returned binary data
Ti.API.debug(this.responseText);
alert(xhr.responseText);
},
onerror: function (e) { // this function is called when an error occurs, including a timeout
Ti.API.debug(e.error);
alert(e.toString);
},
timeout: 5000 /* in milliseconds */
});
xhr.autoEncodeUrl = false;
var params = {
email: $.email.value,
password: $.password.value
};
xhr.open('POST', url);
xhr.send(params); // request is actually sent with this statement
Ti.API.info(xhr.responseText);
};
But I can't do it because receive strange message. I put it in the Logs. Besides, I did another requests and they worked successfully.
Logs:
[ERROR] : TiHttpClient: (TiHttpClient-1) [3529,3529] HTTP Error (org.apache.http.client.HttpResponseException): Not Found
[ERROR] : TiHttpClient: org.apache.http.client.HttpResponseException: Not Found
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:258)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:217)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1287)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:841)
I solved it with JSON.stringify(params) with the sending the request :
function loginClick(e)
{
var url = 'http://...';
var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
// this function is called when data is returned from the server and available for use
// this.responseText holds the raw text return of the message (used for text/JSON)
// this.responseXML holds any returned XML (including SOAP)
// this.responseData holds any returned binary data
Ti.API.debug(this.responseText);
alert(xhr.responseText);
},
onerror: function(e) {
// this function is called when an error occurs, including a timeout
Ti.API.debug(e.error);
alert(this.status);
alert("error" + e.toString);
},
timeout:5000 /* in milliseconds */
});
xhr.autoEncodeUrl = false;
var params = {
'email': $.email.value,
'password' :$.password.value
};
xhr.open('POST', url);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.send(JSON.stringify(params)); // request is actually sent with this statement
};
The "Not Found" exception makes sense. I tried doing a POST with CURL to demonstrate that this isn't caused by Titanium. Your URL is wrong.
> curl --verbose --data "email=test#mail.com&password=dnipro" http://www.assignmentexpert.com/api/v1
* Adding handle: conn: 0x7ffdc4004400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7ffdc4004400) send_pipe: 1, recv_pipe: 0
* About to connect() to www.assignmentexpert.com port 80 (#0)
* Trying 198.72.112.182...
* Connected to www.assignmentexpert.com (198.72.112.182) port 80 (#0)
> POST /api/v1 HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www.assignmentexpert.com
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 35 out of 35 bytes
< HTTP/1.1 404 Not Found
* Server nginx/1.4.7 is not blacklisted
< Server: nginx/1.4.7
< Date: Fri, 25 Apr 2014 16:45:58 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.4.26
< Set-Cookie: updater=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22d0a5527549f76365252a61361cd78e59%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A12%3A%2298.218.93.45%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A11%3A%22curl%2F7.30.0%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1398444358%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D7b00051302d9b00cd28b355be89a1e98; expires=Sun, 24-Apr-2016 16:45:58 GMT; path=/
< Set-Cookie: logged=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT
< Location: /api/v1/
<
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
(truncated -- HTML returned is identical to www.assignmentexpert.com)