#include <linux/module.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/timer.h>

struct my_device_data {
    struct timer_list my_timer;
    struct platform_device *pdev;  // 添加指向 platform_device 的指针
};

void my_timer_callback(struct timer_list *t) {
    struct my_device_data *data = from_timer(data, t, my_timer);
    dev_info(&data->pdev->dev, "hello world\n");  // 通过指针访问设备

    // 重新启动定时器
    mod_timer(&data->my_timer, jiffies + msecs_to_jiffies(10000));
}

static int my_device_probe(struct platform_device *pdev) {
    struct my_device_data *data;
    struct device_node *np = pdev->dev.of_node;

    // 分配设备数据结构
    data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
    if (!data)
        return -ENOMEM;

    // 存储 platform_device 指针
    data->pdev = pdev;

    // 初始化定时器
    timer_setup(&data->my_timer, my_timer_callback, 0);

    // 启动定时器
    mod_timer(&data->my_timer, jiffies + msecs_to_jiffies(10000));

    // 存储设备数据指针
    platform_set_drvdata(pdev, data);

    dev_info(&pdev->dev, "设备匹配: %s\n", np->name);

    return 0;
}

static int my_device_remove(struct platform_device *pdev) {
    struct my_device_data *data = platform_get_drvdata(pdev);

    // 删除定时器
    del_timer_sync(&data->my_timer);

    dev_info(&pdev->dev, "设备移除\n");
    return 0;
}

static const struct of_device_id my_device_ids[] = {
    { .compatible = "mynode", },
    { /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, my_device_ids);

static struct platform_driver my_device_driver = {
    .probe = my_device_probe,
    .remove = my_device_remove,
    .driver = {
        .name = "my_device_driver",
        .of_match_table = my_device_ids,
        .owner = THIS_MODULE,
    },
};

module_platform_driver(my_device_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
MODULE_DESCRIPTION("示例设备树驱动，每10秒打印一次hello world");


private fun addAndUploadPhoto(path: String, context: Context, activity: Activity, ok: (String) -> Unit) {
    "addAndUploadPhoto path:${path}".logDebug()
    uploadTag = path

    val file = File(path)
    val requestBody = RequestBody.create(MediaType.parse("image/*"), file)
    val body = MultipartBody.Part.createFormData("file", file.name, requestBody)

    val client = OkHttpClient()
    val url = "https://your-api-endpoint/admin/sys-file/upload"

    val request = Request.Builder()
        .url(url)
        .post(body)
        .build()

    client.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call, e: IOException) {
            activity.runOnUiThread {
                "图片上传失败".toastShort()
                mLoadingDialog?.dismiss()
                mLoadingDialog = null
            }
        }

        override fun onResponse(call: Call, response: Response) {
            if (response.isSuccessful) {
                val responseBody = response.body()?.string()
                val jsonObject = JSONObject(responseBody)
                val filePath = jsonObject.getString("filePath")

                activity.runOnUiThread {
                    "https://your-api-endpoint/$filePath".logDebug()
                    ok.invoke("https://your-api-endpoint/$filePath")
                    "上传成功".toastShort()
                    mLoadingDialog?.dismiss()
                    mLoadingDialog = null
                }
            } else {
                activity.runOnUiThread {
                    "图片上传失败".toastShort()
                    mLoadingDialog?.dismiss()
                    mLoadingDialog = null
                }
            }
        }
    })
}
